Queues

33
Queues Mustafa K. Uyguroğlu

Transcript of Queues

Page 1: Queues

QueuesMustafa K. Uyguroğlu

Page 2: Queues

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

Queueitems[MAXQUEUE-

1].

. .

. .

. .

items[2] C Rear=2

items[1] B

items[0] A Front=0

Page 4: Queues

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

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

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

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

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

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

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

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

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

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

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

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

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

Initialize the queue.

items[MAXQUEUE-1] front=rear=MAXQUEUE-1

.

.

items[3]

items[2]

items[1]

items[0]

Page 18: Queues

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

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

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

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

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

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

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

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

Example Following program is an example of circular

queue insertion and deletion operation.

Page 27: Queues

#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

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

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

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

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

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

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