Queues

Post on 21-Jul-2016

214 views 2 download

Transcript of Queues

QueuesMustafa K. Uyguroğlu

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

Queueitems[MAXQUEUE-

1].

. .

. .

. .

items[2] C Rear=2

items[1] B

items[0] A Front=0

Declaration of a Queue

# define MAXQUEUE 50 /* size of the queue items*/

typedef struct {int front; int rear;int items[MAXQUEUE];

}QUEUE;

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

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.

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

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

}

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

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

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

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

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.

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

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

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.

Initialize the queue.

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

.

.

items[3]

items[2]

items[1]

items[0]

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

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

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

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

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

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;

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

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

Example Following program is an example of circular

queue insertion and deletion operation.

#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 *);

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

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

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.

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.

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

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