# 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 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:

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. 1 1 1 2 Stack empty Item 1 pushed on stack Item 2 pushed on stack Item 2 popped off stack

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.

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.

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).

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

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.

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; }

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"

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

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

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 { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/13/4023759/slides/slide_11.jpg", "name": "Assertions An assertion is a condition that must always be true at some particular point of the program s execution.", "description": "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

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