UNIT III
• Queue: The queue and its sequential representation, Simple Queue,
Circular Queue, Double ended Queue, Priority Queue, applications of
queue
Queue
• A queue can be defined as an ordered list which enables insert
operations to be performed at one end called REAR and delete
operations to be performed at another end called FRONT.
• One end is always used to insert data (enqueue) and the other is used to
remove data (dequeue).
• Queue follows First-In-First-Out methodology, i.e., the data item stored
first will be accessed first.
2
Queue Real time Example
Difference Between Stack and Queue in
terms of Array Implementation
Array Representation of Queue
Another Condition for Queue emptiness
Cont…..
Finding Number of Elements in the Queue
Queue Overflow
Applications of Queue
• Queues are widely used as waiting lists for a single shared resource like
printer, disk, CPU.
• Queues are used in asynchronous transfer of data (where data is not
being transferred at the same rate between two processes) for eg. pipes,
file IO, sockets.
• Queues are used as buffers in most of the applications like MP3 media
player, CD player, etc.
• Queue are used to maintain the play list in media players in order to add
and remove the songs from the play-list.
• Queues are used in operating systems for handling interrupts.
11
Operations on Queue
• Enqueue: The enqueue operation is used to insert the element at the rear end of
the queue. It returns void.
• Dequeue: The dequeue operation performs the deletion from the front-end of the
queue. It also returns the element which has been removed from the front-end. It
returns an integer value. The dequeue operation can also be designed to void.
• Peek: This is the third operation that returns the element, which is pointed by the
front pointer in the queue but does not delete it.
• Queue overflow (isfull): When the Queue is completely full, then it shows the
overflow condition.
• Queue underflow (isempty): When the Queue is empty, i.e., no elements are in
the Queue then it throws the underflow condition.
12
isempty() isfull()
Algorithm − Algorithm −
begin procedure isempty
if begin procedure isfull
front is less than MIN OR front if rear equals to MAXSIZE
is greater than rear return true
return true Else
else return false
return false end procedure
end procedure
Example
Example:
isfull()
bool isempty() {
{ if( rear == MAXSIZE-1)
if(front < 0 || front > rear) return true;
return true; else
else return false;
return false; } }
13
ENQUEUE operation
• Queues maintain two data
pointers, front and rear.
• Therefore, its operations are
comparatively difficult to implement
than that of stacks.
• Step 1 − Check if the queue is full.
• Step 2 − If the queue is full, produce
overflow error and exit.
• Step 3 − If the queue is not full,
increment rear pointer to point the
next empty space.
• Step 4 − Add data element to the
queue location, where the rear is
pointing.
• Step 5 − return success.
14
Algorithm −
procedure enqueue(data) Example
int enqueue(int data)
if queue is full
if(isfull())
return overflow return 0;
endif rear = rear + 1;
rear ← rear + 1 queue[rear] = data;
queue[rear] ← data return 1;
end procedure
return true
end procedure
15
Dequeue Operation
• Accessing data from the queue is a process of two tasks − access the data
where front is pointing and remove the data after access.
• The following steps are taken to perform dequeue operation −
• Step 1 − Check if the queue is empty.
• Step 2 − If the queue is empty, produce underflow error and exit.
• Step 3 − If the queue is not empty, access the data where front is pointing.
• Step 4 − Increment front pointer to point to the next available data element.
• Step 5 − Return success.
16
Algorithm −
procedure dequeue
if queue is empty Example:
return underflow int dequeue()
end if { if(isempty())
return 0;
data = queue[front]
int data = queue[front];
front ← front + 1 front = front + 1;
return true return data; }
end procedure
17
Types of Queue
• Linear Queue
• Circular Queue
• Priority Queue
• Double ended Queue
18
Linear Queue
• In Linear Queue, an insertion takes place from one end while the deletion occurs from
another end.
• The end at which the insertion takes place is known as the rear end, and the end at which
the deletion takes place is known as front end.
• It strictly follows the FIFO rule.
The above figure shows that the elements are inserted from the rear end, and if we insert more
elements in a Queue, then the rear value gets incremented on every insertion.
If we want to show the deletion, then it can be represented as:
19
Continue….
• The major drawback of using a linear Queue is that insertion is
done only from the rear end.
• If the first three elements are deleted from the Queue, we
cannot insert more elements even though the space is available
in a Linear Queue.
• In this case, the linear Queue shows the overflow condition as
the rear is pointing to the last element of the Queue.
20
Circular Queue
• In Circular Queue, all the nodes are represented as circular.
• It is similar to the linear Queue except that the last element of the queue is
connected to the first element.
• It is also known as Ring Buffer as all the ends are connected to another end.
• The drawback that occurs in a linear queue is overcome by using the circular
queue.
• If the empty space is available in a circular queue, the new element can be
added in an empty space by simply incrementing the value of rear.
• The circular queue can be represented as:
21
Enqueue()
INPUT ITEM IN queue[ ]
if(front==-1&&rear==-1)
front=rear=0
queue[rear]=ITEM
else if ((rear+1)%N==front)
PRINT OVER FLOW
else
rear=(rear+1)%N
queue[rear]=ITEM
Dequeue()
{
if(front==-1&&rear==-1)
PRINT UNDER FLOW
else if(front==rear) // for only one element in queue
front=rear=-1
else
Print dequeued element
front=(front+1)%N
Enqueuefront()
INPUT ITEM IN dqueue[ ]
if((front==0&&rear==N-1)||(front==rear+1))
PRINT OVER FLOW
else if(front==-1&&rear==-1)
front=rear=0;
dqueue[front]=ITEM
else if (front==0)
front=N-1
dqueue[front]=ITEM
else
front--
dqueue[front]=ITEM
Enqueuerear()
INPUT ITEM IN dqueue[ ]
if((front==0&&rear==N-1)||(front==rear+1))
PRINT OVER FLOW
else if(front==-1&&rear==-1)
front=rear=0
dqueue[rear]=ITEM
else if (rear==N-1)
rear=0
dqueue[rear]=ITEM
else
rear++
dqueue[rear]=ITEM
Dequeuefront()
if(front==-1&&rear==-1)
PRINT UNDER FLOW
else if(front==rear) // for only one element in queue
Print dequeued element FROM FRONT
front=rear=-1;
else if(front==N-1)
Print dequeued element FROM FRONT
front=0
else
Print dequeued element FROM FRONT
front=front++
Dequeuerear()
if(front==-1&&rear==-1)
PRINT UNDER FLOW
else if(front==rear) // for only one element in queue
Print dequeued element FROM REAR
front=rear=-1
else if(rear==0)
Print dequeued element FROM REAR
rear=N-1
else
Print dequeued element FROM REAR
rear=rear--
Insert_in_priorityq() //Insert Function
INPUT ITEM IN pqueue[ ]
if (rear==N-1)
PRINT OVER FLOW
if (front==-1&&rear==-1)
front=rear=0
pqueue[rear]=ITEM
return
else
check(ITEM) //1ST GO TO CHECKITEM FUNCTION
rear++ //THEN RETURN
Check(ITEM) / /Check Function - to check priority and place element
{
int i,j;
for (i = 0; i <= rear; i++)
{
if (ITEM >= pqueue[i])
{
for (j = rear + 1; j > i; j--)
{
pqueue[j] = pqueue[j - 1]
}
pqueue[i] = ITEM
return
}
pqueue[i] = ITEM
}
}
Delete_by_priority() //Delete Function
if (front==-1&&rear==-1)
PRINT UNDER FLOW
else if (front==rear)
deleted element is with higher priority", pqueue[front]);
front=rear=-1
else
deleted element is with higher priority", pqueue[front]);
front++