Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Queues and Lists. 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.

Similar presentations


Presentation on theme: "1 Queues and Lists. 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."— Presentation transcript:

1 1 Queues and Lists

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

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

4 Queue ADT 4 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;

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

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

7 Implementation of Queue 7 queue create ( int n ) { queue q; q.size = n; q.front = -1; /* empty queue has both front and rear == -1 */ q.rear = -1; /* the array of items is viewed as circular */ q.items = (elements*)calloc(n,sizeof(elements)); return q; } int empty ( queue q ) { return (q.rear == -1 ? 1 : 0); }

8 Implementation of Queue 8 int full ( queue q ){ return (q.front == (q.rear+1) % q.size ? 1 : 0); } elements delete ( queue *q ){ elements e; assert(empty(*q)==0); /* enforce precondition */ if (q->front == q->rear) q->rear = -1; /* queue will be empty */ e = q->items[q->front]; if (q->rear == -1) q->front = -1; /* queue is empty */ else q->front = (q->front+1) % q->size; return e; } B C 01234 front rear G C D E 01234 FrontRear

9 Implementation of Queue 9 void insert ( queue *q, elements e ) { assert(full(*q)==0); /* enforce precondition */ q->rear = (q->rear+1) % q->size; q->items[q->rear] = e; if (q->front == -1) /* queue was empty */ q->front = 0; /* queue of one item */ } void clear ( queue *q ) { free(q->items); q->size = 0; q->front = -1; q->rear = -1; } B C 01234 front rear

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

11 11 Insertion into a Linked List Insert 9 after 5 6538 HeadNull 9

12 12 Deletion from a Linked List Delete 3 from the list 6538 HeadNull 9 Free this space

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

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

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

16 16 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

17 17 Inserting at the Head void inserthead(node *head, int a) { node *ptr; ptr->info = a; ptr->next = head; head = ptr; } 6538 Head Null 2 6538 Head inserthead(head,2); Memory Problems

18 18 Inserting at the Head void inserthead(node *head, int a) { node *ptr; ptr = (node*)malloc(sizeof(node)); ptr->info = a; ptr->next = head; head = ptr; } 6538 Head Null 2 6538 Head inserthead(head,2); Can not modify 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 Removing at the Head void deletehead(node *head) { head = head->next; return; } 6538 Head Null 538 Head Null deletehead(head); Memory Leak

22 22 Removing at the Head void deletehead(node *head) { node * ptr; ptr = head; head = head->next; free(ptr); return; } 6538 Head Null 538 Head Null deletehead(head); Can not modify head

23 23 Removing at the Head node* deletehead(node *head) { node * ptr; ptr = head; head = head->next; free(ptr); return(head); } 6538 Head Null 538 Head Null head = deletehead(head);

24 24 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

25 25 Inserting at the Tail node *inserttail(node *head, int a) { node *ptr; node *ptr2 = head; ptr = (node*)malloc(sizeof(node)); ptr->info = a; ptr->next = NULL; if (head == NULL) return (ptr); else if (head->next == NULL) { head->next = ptr; return (head); } while (head->next != NULL) head = head->next; head->next = ptr; return(ptr2); } 6538 Head Null

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

27 27 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);

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

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

30 Linked List Implementation of Queue 30 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

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

32 Linked List Implementation of Queue 32 6538 front Null int empty ( queue q ) { if (q.front == NULL) return (1); else return (0); } rear

33 Linked List Implementation of Queue 33 6538 front Null void insert ( queue *q, elements e ) { node *l; l = (node*)malloc(sizeof(node)); l->info = e; l->next = NULL; rear if (empty(*q) == 1) q->front = l; else q->rear->next = l; q->rear = l; }

34 Linked List Implementation of Queue 34 6538 front Null elements delete ( queue *q ) { elements e; node *l; assert(empty(*q)==0); l = q->front; e = l->info; l = l->next; rear free(q->front); q->front = l; return e; }

35 35 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

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

37 37 Inserting at the Head cnode *inserthead(cnode *head, int a) { cnode *ptr; ptr = (cnode*)malloc(sizeof(cnode)); ptr->info = a; ptr->next = head; head->previous = ptr; ptr->previous = NULL; return(ptr); } 538 Head Null 6538 Head Null

38 38 Delete a given Node void delete(cnode *p, int *px) { cnode *q,r; if (p== NULL) { printf(“void deletion\n”); return; } *px = p->info; q = p->previous; r = p->next; 6538 Null Head Null q->next = r; r->previous = q; free(p); return; } p q r

39 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

40 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

41 Queues as Circular Lists #define elements int struct node { elements info; struct node *next; }; typedef struct node node; 6538 list First node Last node node *insert_item ( node *l, elements i ); /* adds the element i to the rear of the list */ node *delete_item ( node *l, elements *i ); /* deletes the first element from the list */

42 Queues as Circular Lists node *insert_item ( node *l, int i ) { node *nl; nl = (node*)malloc(sizeof(node)); nl->info = i; if (l == NULL) nl->next = nl; else 6538 l { nl->next = l->next; l->next = nl; } return nl; }

43 Queues as Circular Lists node *delete_item ( node *l, int *i ) { assert(l != NULL); if (l->next == l) { *i = l->info; free(l); return NULL; } 6538 l else { node *head = l->next; *i = head->info; l->next = head->next; free(head); return l; }

44 Print a Circular List void print ( node *l ) { if (l != NULL) { node *p; for (p = l->next; p != l; p = p->next) printf(" %d", p->info); printf(" %d", l->info); } 6538 list

45 Exercises Write a function to create a copy of a linked list. Write a function to free the nodes of a linked list. Write a function to reverse a singly linked list Write a function to insert a number into a sorted linked list Write a function to remove duplicate elements in a sorted linked list


Download ppt "1 Queues and Lists. 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."

Similar presentations


Ads by Google