Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advanced Data Structures Stack –a stack is dynamic data items in a linear order, such that the item first "pushed" in is the last item "popped" out. Think.

Similar presentations


Presentation on theme: "Advanced Data Structures Stack –a stack is dynamic data items in a linear order, such that the item first "pushed" in is the last item "popped" out. Think."— Presentation transcript:

1 Advanced Data Structures Stack –a stack is dynamic data items in a linear order, such that the item first "pushed" in is the last item "popped" out. Think of stack as a stack of books, you are only allowed to put a book on top of the stack or take a book from top of the stack Stack empty Item 1 pushed on stack Item 2 pushed on stack Item 2 popped off stack

2 Advanced Data Structures Queue –a queue is dynamic data items in a linear order, such that the item first goes in is the first item goes out. Think of a queue as a queue of people. Item goes in from one end and comes out from the other end.

3 Advanced Data Structures Tree –a tree is dynamic data items in an order similar to the hierarchical structure of tree in nature. Each "node" can have a number of "children". Each child can have a number of children and so on.

4 Structure The stack, queue, or tree data structure can be implemented with structure and pointer to structure. The basic element or node is of type struct node { int num;...; struct node *pt; }; Any number of information fields. One or more pointers to the same structure (self- referential).

5 Linked Lists A (linked) list of names of elephants can be represented with the structure: struct elephant { char name[10]; struct elephant *next; }; start = &e1; e1.next = &e2; e2.next = &e3; e3.next = NULL; start "Edna" "Elmer""Eloise" e1e2e3 name next name next name next

6 Why Use Linked List? The linked list has advantage over fixed-size array –The list can be extended by inserting a node anywhere in the list. –The list can be shortened by deleting a node. –The list can be used to implement a stack or queue.

7 Print a Linked List #include typedef struct elephant { char name[10]; struct elephant *next; } ELEPHANT; void print_elephants(const ELEPHANT* ptr); main() { ELEPHANT e1, e2, e3, *start; strcpy(e1.name, "Edna"); strcpy(e2.name, "Elmer"); strcpy(e3.name, "Eloise"); start = &e1; e1.next = &e2; e2.next = &e3; e3.next = NULL; print_elephants(start); } void print_elephants(const ELEPHANT* ptr) { int count = 1; printf("\n"); while(ptr != NULL) { printf("\nElephant numer %d is %s.", count++, ptr -> name); ptr = ptr -> next; }

8 Add a Node in front of the List  start e1 e2 new new = malloc(sizeof(ELEPHANT)); new->next = start; strcpy(new->name, "Tha"); start = new; "Tha" "Edna""Elmer"

9 Delete a Node old = ptr -> next; ptr->next = old->next; free(old); ptr old

10 Binary Tree struct tree { int nums; struct tree *left, *right; }

11 Assertions An assertion is a condition that must always be true at some particular point of the program's execution. Assertions, which are embedded in the code, are checked for correctness when the program is running. When an assertion fails, the program terminates and issue error messages. In C, we can put in assertion by #include... assert(condition); where the condition is any single expression, nonzero for true, 0 for false. Example: assert(a

12 C Language Features not Discussed Some of the preprocessing directives like #pragma, and stringization #x. Pointers to functions. Functions with variable number of arguments. Some less commonly used functions in standard libraries such as,,,... Exception-handling and jumps.


Download ppt "Advanced Data Structures Stack –a stack is dynamic data items in a linear order, such that the item first "pushed" in is the last item "popped" out. Think."

Similar presentations


Ads by Google