Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.

Similar presentations


Presentation on theme: "1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain."— Presentation transcript:

1 1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain how to do things using stacks, queues, lists etc

2 2 Stack

3 The Stack Metaphor A stack is a data structure in which the elements are accessible only in a last- in/first-out order. The fundamental operations on a stack are push, which adds a new value to the top of the stack, and pop, which removes and returns the top value. One of the most common metaphors for the stack concept is a spring-loaded storage tray for dishes. Adding a new dish to the stack pushes any previous dishes downward. Taking the top dish away allows the dishes to pop back up.

4 Implementation of Stack #define elements int #define stacksize 100 typedef struct { int top; elements items[stacksize]; } stack; 4

5 Implementation of Stack int empty ( stack s ); /* postcondition: empty(s) == 1 if s is empty, == 0 otherwise */ int full ( stack s ); /* postcondition: full(s) == 1 if s is full, == 0 otherwise */ elements pop ( stack *s ); /* precondition: not empty(*s); postcondition: push(*s, pop(*s)) == *s */ void push ( stack *s, elements e ); /* precondition: full(*s) == 0; postcondition: push(*s,e); pop(*s) == e */ 5

6 Implementation of Stack II #define elements int typedef struct { int top; int size; elements *items; } stack; 6

7 Implementation of Stack II stack create ( int n ); /* postcondition: create(n) is a stack which can hold n items */ int empty ( stack s ); /* postcondition: empty(s) == 1 if s is empty, == 0 otherwise */ int full ( stack s ); /* postcondition: full(s) == 1 if s is full, == 0 otherwise */ int pop ( stack *s, elements *e ); /* precondition: not empty(*s); postcondition: pop(*s,*e) == 0 if not empty(*s), == 1 otherwise, check = push(*s,*e) restores the stack */ 7

8 8 Queues and Lists

9 QUEUES Very similar to stacks The only difference between them is in the order in which elements are processed. A stack uses a last-in/first-out (LIFO) discipline A queue adopts a first-in/first-out (FIFO) model that more closely resembles a waiting line. 9

10 Queue ADT abstract typedef > queue; /* a queue is a sequence of elements, FIFO = First In First Out */ abstract int empty ( queue q ); postcondition: empty(s) == 1 if queue is empty, == 0 otherwise; abstract int full ( queue q ); postcondition: full(s) == 1 if queue is full, == 0 otherwise; 10

11 Queue ADT 11 abstract elements delete ( queue q ); /* dequeue */ precondition: empty(q) == 0; postcondition: first element is removed from queue q; abstract void insert ( queue q, elements e ); /* enqueue */ precondition: full(q) == 0; postcondition: element e is added to the end of queue q;

12 Implementation of Queue 12 #define elements int typedef struct { int size,front,rear; elements *items; } queue;

13 Implementation of Queue 13 01234 FrontRear B C 01234 FrontRear C D E 01234 FrontRear C 01234 FrontRear

14 14 Linked List Group of nodes connected by pointers A node consists of Data Pointer to next node 6538 HeadNull

15 15 Declaration of a node struct node { int info; struct node *next; }; typedef struct node node; 6538 HeadNull

16 16 Linked List Structure 6538 Head 6538 Null Some address In memory Points back to Internal node

17 17 Dynamic allocation of a node node *ptr; ptr = (node *)malloc(sizeof(node)); ? ptr free(ptr)

18 18 Inserting at the Head 1. Allocate a new node 2. Insert new element 3. Make new node point to old head 4. Update head to point to new node 6538 HeadNull 2 6538 Head

19 19 Inserting at the Head node *inserthead(node *head, int a) { node *ptr; ptr = (node*)malloc(sizeof(node)); ptr->info = a; ptr->next = head; return(ptr); } 6538 Head Null 2 6538 Head head = inserthead(head,2);

20 20 Removing at the Head 1. Update head to point to next node in the list 2. Allow garbage collector to reclaim the former first node 6538 Head Null 538 Head Null

21 21 Inserting at the Tail 1. Allocate a new node 2. Insert new element 3. Have new node point to null 4. Have old last node point to new node 6538 Head Null 6538 Head Null 1

22 22 Print a linked list void printlist (node *head) { while (head !=NULL) { printf("%d ",head->info); head = head->next; } printf("\n"); } 6538 HeadNull printlist(head);

23 23 Find length of a linked list int length(node *head) { if (head == NULL) return 0; else return 1 + length(head->next); } 6538 HeadNull x=length(head);

24 Linked List Implementation of Queue typedef struct { int size,front,rear; elements *items; } queue; 24 6538 HeadNull

25 Linked List Implementation of Queue struct node { elements info; struct node *next; }; typedef struct node node; 25 6538 front Null struct queue { node *front; node *rear; } ; typedef struct queue queue rear

26 Linked List Implementation of Queue 26 6538 front Null int empty ( queue q ); /* postcondition: empty(q) == 1 if queue q is empty, * == 0 otherwise; */ elements delete ( queue *q ); /* precondition: empty(*q) == 0; * postcondition: first element is removed from queue *q; */ void insert ( queue *q, elements e ); /* precondition: full(*q) == 0; * postcondition: element e is inserted to end of queue *q; */ rear

27 Linked List Implementation of Queue 27 6538 Null queue *q; q frontrear struct

28 28 Doubly Linked List Group of nodes connected by pointers A node consists of Data Pointer to next node Pointer to previous node 6538 Head Null

29 29 Doubly Linked List struct cnode { int info; struct cnode *next; struct cnode *previous; }; typedef struct cnode cnode; 6538 Head Null

30 Circular Lists Linked lists have some limitations Given a pointer p, we can not reach preceding nodes In circular lists, next field of last node points to first node A node consists of Data Pointer to next node 6538 HeadNull 6538 Head

31 Circular Lists Keep a pointer to the last node How can we add or remove an element from either the front or the rear of the list? How can we check if the list is empty? 6538 list First node Last node


Download ppt "1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain."

Similar presentations


Ads by Google