# 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 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 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 (LIFO) discipline A queue adopts a first-in/first-out (FIFO) model that more closely resembles a waiting line. 2

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

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;

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

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

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

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

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 Linked List Group of nodes connected by pointers A node consists of Data Pointer to next node 6538 HeadNull

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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 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 Doubly Linked List struct cnode { int info; struct cnode *next; struct cnode *previous; }; typedef struct cnode cnode; 6538 Head Null

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

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

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

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 */

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

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

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

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