Queues
Transcript of Queues
![Page 1: Queues](https://reader031.fdocuments.us/reader031/viewer/2022020520/577cc7641a28aba711a0cd25/html5/thumbnails/1.jpg)
QueuesMustafa K. Uyguroğlu
![Page 2: Queues](https://reader031.fdocuments.us/reader031/viewer/2022020520/577cc7641a28aba711a0cd25/html5/thumbnails/2.jpg)
DEFINITION OF QUEUE A Queue is an ordered collection of items from which items may
be deleted at one end (called the front of the queue) and into which items may be inserted at the other end (the rear of the queue).
The first element inserted into the queue is the first element to be removed. For this reason a queue is sometimes called a fifo (first-in first-out) list as opposed to the stack, which is a lifo (last-in first-out).
![Page 3: Queues](https://reader031.fdocuments.us/reader031/viewer/2022020520/577cc7641a28aba711a0cd25/html5/thumbnails/3.jpg)
Queueitems[MAXQUEUE-
1].
. .
. .
. .
items[2] C Rear=2
items[1] B
items[0] A Front=0
![Page 4: Queues](https://reader031.fdocuments.us/reader031/viewer/2022020520/577cc7641a28aba711a0cd25/html5/thumbnails/4.jpg)
Declaration of a Queue
# define MAXQUEUE 50 /* size of the queue items*/
typedef struct {int front; int rear;int items[MAXQUEUE];
}QUEUE;
![Page 5: Queues](https://reader031.fdocuments.us/reader031/viewer/2022020520/577cc7641a28aba711a0cd25/html5/thumbnails/5.jpg)
QUEUE OPERATIONS Initialize the queue Insert to the rear of the queue Remove (Delete) from the front of the
queue Is the Queue Empty Is the Queue Full What is the size of the Queue
![Page 6: Queues](https://reader031.fdocuments.us/reader031/viewer/2022020520/577cc7641a28aba711a0cd25/html5/thumbnails/6.jpg)
INITIALIZE THE QUEUE
items[MAXQUEUE-1]
.
.
.items[1]items[0] front=0
rear=-1
•The queue is initialized by having the rear set to -1, and front set to 0. Let us assume that maximum number of the element we have in a queue is MAXQUEUE elements as shown below.
![Page 7: Queues](https://reader031.fdocuments.us/reader031/viewer/2022020520/577cc7641a28aba711a0cd25/html5/thumbnails/7.jpg)
Queue a new item (D) is inserted at the Rear of the
queue
items[MAXQUEUE-1]
. .
. .items[3] D Rear=3items[2] Citems[1] Bitems[0] A Front=0
![Page 8: Queues](https://reader031.fdocuments.us/reader031/viewer/2022020520/577cc7641a28aba711a0cd25/html5/thumbnails/8.jpg)
Insert Operationvoid insert(QUEUE *qptr, int x){
if(qptr->rear == MAXQUEUE-1){
printf("Queue is full!");exit(1);
}else{qptr->rear++;qptr->items[qptr->rear]=x;}
}
![Page 9: Queues](https://reader031.fdocuments.us/reader031/viewer/2022020520/577cc7641a28aba711a0cd25/html5/thumbnails/9.jpg)
Queue an item (A) is removed (deleted) from the
Front of the queue
items[MAXQUEUE-1]
. .
. .items[3] D Rear=3items[2] Citems[1] B Front=1items[0] A
![Page 10: Queues](https://reader031.fdocuments.us/reader031/viewer/2022020520/577cc7641a28aba711a0cd25/html5/thumbnails/10.jpg)
Remove Operationint remove(struct queue *qptr){ int p;if(qptr->front > qptr->rear){printf("Queue is empty");exit(1);}else{p=qptr->items[qptr->front];qptr->front++;return p;}}
![Page 11: Queues](https://reader031.fdocuments.us/reader031/viewer/2022020520/577cc7641a28aba711a0cd25/html5/thumbnails/11.jpg)
INSERT / REMOVE ITEMS
Remove two items from the front of the queue.
items[MAXQUEUE-1]
. .
. .items[3] D Front=Rear=3
items[2] Citems[1] Bitems[0] A
![Page 12: Queues](https://reader031.fdocuments.us/reader031/viewer/2022020520/577cc7641a28aba711a0cd25/html5/thumbnails/12.jpg)
INSERT / REMOVE ITEMS Assume that the rear= MAXQUEUE-1
•What happens if we want to insert a new item into the queue?
items[MAXQUEUE-1] X rear=MAXQUEUE-1
. .
. .
items[3] D front=3
items[2] C
items[1] B
items[0] A
![Page 13: Queues](https://reader031.fdocuments.us/reader031/viewer/2022020520/577cc7641a28aba711a0cd25/html5/thumbnails/13.jpg)
INSERT / REMOVE ITEMS What happens if we want to insert a new
item F into the queue? Although there is some empty space, the
queue is full. One of the methods to overcome this
problem is to shift all the items to occupy the location of deleted item.
![Page 14: Queues](https://reader031.fdocuments.us/reader031/viewer/2022020520/577cc7641a28aba711a0cd25/html5/thumbnails/14.jpg)
REMOVE ITEMitems[MAXQUEUE-1]
. .
. .
items[3] D Rear=3
items[2] C
items[1] B
items[0] A
items[MAXQUEUE-1]
. .
. .
items[3]
items[2] D Rear=2
items[1] C
items[0] B
![Page 15: Queues](https://reader031.fdocuments.us/reader031/viewer/2022020520/577cc7641a28aba711a0cd25/html5/thumbnails/15.jpg)
Modified Remove Operationint remove(struct queue *qptr){ int p,i;if(qptr->front > qptr->rear){printf("Queue is empty");exit(1);}else{p=qptr->items[qptr->front];for(i=1;i<=qptr->rear;i++)qptr->items[i-1]=qptr->items[i];qptr->rear--return p;} }
![Page 16: Queues](https://reader031.fdocuments.us/reader031/viewer/2022020520/577cc7641a28aba711a0cd25/html5/thumbnails/16.jpg)
INSERT / REMOVE ITEMS Since all the items in the queue are required
to shift when an item is deleted, this method is not preferred.
The other method is circular queue. When rear = MAXQUEUE-1, the next
element is entered at items[0] in case that spot is free.
![Page 17: Queues](https://reader031.fdocuments.us/reader031/viewer/2022020520/577cc7641a28aba711a0cd25/html5/thumbnails/17.jpg)
Initialize the queue.
items[MAXQUEUE-1] front=rear=MAXQUEUE-1
.
.
items[3]
items[2]
items[1]
items[0]
![Page 18: Queues](https://reader031.fdocuments.us/reader031/viewer/2022020520/577cc7641a28aba711a0cd25/html5/thumbnails/18.jpg)
Insert items into circular queue Insert A,B,C to the rear of the queue.
items[MAXQUEUE-1] front=MAXQUEUE-1
.
.
items[3]
items[2] C rear=2
items[1] B
items[0] A
![Page 19: Queues](https://reader031.fdocuments.us/reader031/viewer/2022020520/577cc7641a28aba711a0cd25/html5/thumbnails/19.jpg)
Remove items from circular queue Remove two items from the queue.
items[MAXQUEUE-1]
.
.items[3]
items[2] C rear=2
items[1] B front=1
items[0] A
![Page 20: Queues](https://reader031.fdocuments.us/reader031/viewer/2022020520/577cc7641a28aba711a0cd25/html5/thumbnails/20.jpg)
Insert D,E to the queue.
items[MAXQUEUE-1]
.items[4] E rear=4
items[3] D
items[2] C
items[1] B front=1
items[0] A
![Page 21: Queues](https://reader031.fdocuments.us/reader031/viewer/2022020520/577cc7641a28aba711a0cd25/html5/thumbnails/21.jpg)
Insert items (rear=0)
items[MAXQUEUE-1] X
.items[4] E
items[3] D
items[2] C
items[1] B front=1
items[0] Y rear=0
![Page 22: Queues](https://reader031.fdocuments.us/reader031/viewer/2022020520/577cc7641a28aba711a0cd25/html5/thumbnails/22.jpg)
Insert Z to the queue. (queue is full!)
items[MAXQUEUE-1] X
. .items[4] E
items[3] D
items[2] C
items[1] ?? front=rear=1
items[0] Y
![Page 23: Queues](https://reader031.fdocuments.us/reader031/viewer/2022020520/577cc7641a28aba711a0cd25/html5/thumbnails/23.jpg)
Declaration and Initialization of a Circular Queue.
#define MAXQUEUE 10 /* size of the queue items*/
typedef struct {int front;int rear;int items[MAXQUEUE];
}QUEUE;QUEUE q;q.front = MAXQUEUE-1;q.rear= MAXQUEUE-1;
![Page 24: Queues](https://reader031.fdocuments.us/reader031/viewer/2022020520/577cc7641a28aba711a0cd25/html5/thumbnails/24.jpg)
Insert Operationfor circular Queue
void insert(QUEUE *qptr, int x){if(qptr->rear == MAXQUEUE-1)
qptr->rear=0;else
qptr->rear++;if(qptr->rear == qptr->front){
printf("Queue overflow");exit(1);
}qptr->items[qptr->rear]=x;}
![Page 25: Queues](https://reader031.fdocuments.us/reader031/viewer/2022020520/577cc7641a28aba711a0cd25/html5/thumbnails/25.jpg)
Remove Operationfor circular queue
int remove(struct queue *qptr){if(qptr->front == qptr->rear){
printf("Queue underflow");exit(1);
}if(qptr->front == MAXQUEUE-1)
qptr->front=0;else
qptr->front++;return qptr->items[qptr->front];}
![Page 26: Queues](https://reader031.fdocuments.us/reader031/viewer/2022020520/577cc7641a28aba711a0cd25/html5/thumbnails/26.jpg)
Example Following program is an example of circular
queue insertion and deletion operation.
![Page 27: Queues](https://reader031.fdocuments.us/reader031/viewer/2022020520/577cc7641a28aba711a0cd25/html5/thumbnails/27.jpg)
#include <stdlib.h>#include <stdio.h>#define MAXELEMENTS 50#define TRUE 1#define FALSE 0typedef struct { int items[MAXELEMENTS]; int front , rear ; } QUEUE; void qinsert( QUEUE * , int); int qdelete(QUEUE *); int empty(QUEUE *);
![Page 28: Queues](https://reader031.fdocuments.us/reader031/viewer/2022020520/577cc7641a28aba711a0cd25/html5/thumbnails/28.jpg)
int main() { char operation; int x; QUEUE q; q.front = q.rear = MAXELEMENTS - 1; do { printf("%s\n","Insert Operation type I(nsert) D(elete) or E(xit) "); scanf("\n%c",&operation); switch (operation) { case 'I' :case 'i':printf("%s\n","Insert an element");
scanf("\n%d",&x); qinsert(&q , x); break;
case 'D' :case 'd':x=qdelete(&q); printf("\n %d is deleted \n",x); break;
default: printf(“Incorrect Opeartion type\n”); break; } } while (operation != 'E'&&operation!='e'); return 0;}
![Page 29: Queues](https://reader031.fdocuments.us/reader031/viewer/2022020520/577cc7641a28aba711a0cd25/html5/thumbnails/29.jpg)
int empty(QUEUE *qptr){ return((qptr->front == qptr->rear) ? TRUE : FALSE);}int qdelete(QUEUE *qptr){ if (empty(qptr)) {
printf("Queue underflow ");exit(1);}
qptr->front=(qptr->front+1)%(MAXELEMENTS)return(qptr->items[qptr->front]); } void qinsert(QUEUE *qptr , int x) { /* make room for new element */ printf("\n %d is inserted \n",x);qptr->rear=(qptr->rear+1)%(MAXELEMENTS)
if (qptr->rear == qptr->front) { printf("Queue overflow"); exit(1); }
qptr->items[qptr->rear] = x; return; }
![Page 30: Queues](https://reader031.fdocuments.us/reader031/viewer/2022020520/577cc7641a28aba711a0cd25/html5/thumbnails/30.jpg)
PRIORITY QUEUES The priority queue is a data structure in which
intrinsic ordering of the elements determines the results of its basic operations.
An ascending priority queue is a collection of items into which items can be inserted arbitrarily and from which only the smallest item can be removed. On the other hand a descending priority queue allows only the largest item to be removed.
![Page 31: Queues](https://reader031.fdocuments.us/reader031/viewer/2022020520/577cc7641a28aba711a0cd25/html5/thumbnails/31.jpg)
PRIORITY QUEUES How do we implement insert and delete? Remember that members of a queue need
not be numbers or characters which can be compared directly.
![Page 32: Queues](https://reader031.fdocuments.us/reader031/viewer/2022020520/577cc7641a28aba711a0cd25/html5/thumbnails/32.jpg)
Array implementation of PQ Insertion
note order of insertion is not tampered with Deletion
requires a search for the element of highest priority
what happens if we delete an element in the centre of the array
![Page 33: Queues](https://reader031.fdocuments.us/reader031/viewer/2022020520/577cc7641a28aba711a0cd25/html5/thumbnails/33.jpg)
Possible Solutions … An empty indicator replaces deleted
elements. When the array is full compact the array to the front. Inefficient in search.
Use the empty indicator and insert into the empty slots. Inefficient in insert.
On each deletion move elements up the array, decrementing rear.
Maintain array as an ordered circular array