Advanced Data Structures Stack a stack is dynamic

  • Slides: 12
Download presentation
Advanced Data Structures • Stack – a stack is dynamic data items in a

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

Advanced Data Structures • Queue – a queue is dynamic data items in a

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

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

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 }; Any number of information fields. *pt; One or more pointers to the same structure (selfreferential).

Linked Lists • A (linked) list of names of elephants can be represented with

Linked Lists • A (linked) list of names of elephants can be represented with the structure: struct elephant { char name[10]; struct elephant *next; }; start e 1 e 2 "Elmer" "Edna" name e 3 next start = e 1. next e 2. next e 3. next name &e 1; = &e 2; = &e 3; = NULL; "Eloise" next name next

Why Use Linked List? • The linked list has advantage over fixed-size array –

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 <stdio. h> #include <stdlib. h> #include <string. h> typedef

Print a Linked List #include <stdio. h> #include <stdlib. h> #include <string. h> typedef struct elephant { char name[10]; struct elephant *next; } ELEPHANT; void print_elephants(const ELEPHANT* ptr); main() { ELEPHANT e 1, e 2, e 3, *start; strcpy(e 1. name, "Edna"); strcpy(e 2. name, "Elmer"); strcpy(e 3. name, "Eloise"); start = &e 1; e 1. next = &e 2; e 2. next = &e 3; e 3. next = NULL; print_elephants(start); } void print_elephants(const ELEPHANT* ptr) { int count = 1; printf("n"); while(ptr != NULL) { printf("n. Elephant numer %d is %s. ", count++, ptr -> name); ptr = ptr -> next; } }

Add a Node in front of the List start "Tha" e 1 "Edna" e

Add a Node in front of the List start "Tha" e 1 "Edna" e 2 "Elmer" new = malloc(sizeof(ELEPHANT)); new->next = start; strcpy(new->name, "Tha"); new start = new;

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

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

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

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

Assertions • An assertion is a condition that must always be true at some

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. h>. . . assert(condition); • where the condition is any single expression, nonzero for true, 0 for false. • Example: assert(a<b);

C Language Features not Discussed • Some of the preprocessing directives like #pragma, and

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 <stdio. h>, <stdlib. h>, <time. h>, . . . • Exception-handling and jumps.