Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 15

Queues

• Definition of a Queue
• Examples of Queues
• Design of a Queue
• Deques
Definition of a Queue
• A queue is a data structure that models/enforces the
first-come first-serve order, or equivalently the
first-in first-out (FIFO) order.
• That is, the element that is inserted first into the
queue will be the element that will deleted first, and
the element that is inserted last is deleted last.
• A waiting line is a good real-life example of a
queue.
A Graphic Model of a Queue

Front:
Rear:
All items are
All new items
deleted from
are added on
this end
this end
Operations on Queues
• Insert (item): (enqueue)
– It adds a new item to the rear of the queue
• Remove ( ): (delete or dequeue)
– It deletes the front item of the queue, and returns to the caller. If
the queue is already empty, this operation returns NULL
• getFront( ):
– Returns the value in the head element of the queue
• getRear( ):
– Returns the value in the tail element of the queue
• QEmpty( )
– Returns true if the queue has no items
• Qfull()
– Returns true if Queue is full.
• size( )
– Returns the number of items in the queue
Examples of Queues
• An electronic mailbox is a queue
– The ordering is chronological (by arrival
time).
• A waiting line in a store, at a service
counter, on a one-lane road.
• Equal-priority processes waiting to run on
a processor in a computer system.
• And many more.
How Front and Rear Change
• Front increases by 1 after each dequeue( )
• Rear increases by 1 after each enqueue( )

Rear Front
Now:
49 48 47 4 3 2 1 0
Rear Front
After enqueue:
49 48 47 4 3 2 1 0
Rear Front
After dequeue:
49 48 47 4 3 2 1 0
False-Overflow Issue
• Suppose 50 calls to enqueue have been made, so now the
queue array is full

49 48 47 4 3 2 1 0
Assume 4 calls to dequeue( ) are made

49 48 47 4 3 2 1 0
• Assume a call to enqueue( ) is made now. The Rear part
seems to have no space, but the front has 4 unused spaces; if
never used, they are wasted.
Solution: A Circular Queue
• Allow the Front (and the Rear) to be moving targets
• When the Rear end fills up and front part of the array has
empty slots, new insertions should go into the front end

Rear Front

49 48 47 4 3 2 1 0
• Next insertion goes into slot 0, and Rear tracks it. The
insertion after that goes into a lot 1, etc.
Illustration of Circular Queues
• Current state:
Front

49 48 47 4 3 2 1 0
Rear
• After One Call to enqueue()
Front Rear

49 48 47 4 3 2 1 0

• After One Call to enqueue() Front Rear

49 48 47 4 3 2 1 0
Numerics for Circular Queues
• Front increases by (1 modulo capacity) after
each dequeue( ):
Front = (Front +1) % capacity;
• Rear increases by (1 modulo capacity) after each
enqueue( ):
Rear = (Rear +1) % capacity;
• Special Conditions
– Queue Empty
– Queue Full
Design of Queue
• void enqueue( int a[], int x)
{
if(front==-1 && rear == -1)
{ front=0; rear = 0; a[rear]=x; return;}
if(front%SIZE ==(rear+1)%SIZE)
{ printf(“The Queue is full”); return;}
rear =(rear+1)%SIZE;
a[rear]=x;
}
Design of Queue
• int dequeue(int a[])
{
int x;
if(rear%SIZE==(front+1)%SIZE )
{x=a[front];front=-1; rear =-1; return x;}
if (front== -1)
{printf(“Empty Queue”); return;}
else {x=a[front]; front=(front+1)%SIZE; return x;}
}
Design of Queue
• void display(int a[])
{
int i;
if(rear!=-1 && front!=-1)
{
printf(“\n\n\nThe Contents of Queue is \n”);
for( i=front;i<(rear-1)%SIZE, i=(i+1)%SIZE)
printf(“%d\t”, a[i]);
}
}
Design of Queue
#include<stdio.h>
int front = -1; rear = -1, SIZE=10, a[10]={0};
void main(int a[])
{
int p;
enqueue(a, 12);
enqueue(a, 20);
enqueue(a, 30)
display(a);
p=dequeue(a);
printf(“\nThe element deleted from queue is %d”, p);
display(a);
}
Deques
• A deque is a double-ended queue—items can be
inserted and removed at either end
• Implementation is similar to that for queues
• Two versions – Input restricted and Output restricted

You might also like