2. Queue
A queue is a linear Data structure in which insertion is performed at one end called rear and deletion is
performed at another end of the list is called front.
Type of Queue:
1.Simple Queue
2.Circularqueue
3.Dequeue
4.Priority Queue
Representation of Queue
The information in such a list is proceeds in the same order as it was received.
Since insertion is performed at one end and deletion is performed at another end the element which is inserted
first is first to delete. So it is also known as First in First out (FIFO) or First Come First Serve (FCFS) data structure.
3. Examples of Queue
A raw of students at registration counter.
Line of cars waiting to proceeds in some direction at traffic signal.
Application of Queue
o A queue is the natural data structure for a system to serve its incoming requests. Most of
the process scheduling or disk scheduling algorithms in operating systems use queues.
o Computer hardware like a processor or a network card also maintain buffers in the form of
queues for incoming resource requests. A stack-like data structure causes starvation of the
first requests, and is not applicable in such cases.
o A mailbox or port to save messages to communicate between two users or processes in a
system is essentially a queue-like structure.
o Queue is widely used in Simulation.
4. Operation on Simple Queue:
(1) Insert an element in to queue
(2) Delete an element from queue
1. Algorithm to insert an element in queue
PROCEDURE QINSERT (Q, F, R, N, Y)
• This function insert an element into the queue
• The Queue is represented by vector Q which contains N elements.
• F is a pointer which points to the front end
• R is a pointer which points to the rear end
• Y is the element to be inserted.
1) [Overflow?]
If R≥N then
Write (‘Overflow’)
5. 2) [Increment rear pointer ]
RR+1
3) [Insert element]
Q[R]Y
4 ) [Is front pointer properly set?]
If F=0 then
F1
Return
6. 2. Algorithm to delete an element from the queue
PROCEDURE QDELETE (Q, F, R)
• This function delete an element into the queue
• The Queue is represented by vector Q which contains N elements.
• F is a pointer which points to the front end
• R is a pointer which points to the rear end
• Y is the element to be inserted.
1) [Overflow?]
If R≥N then
Write (‘Overflow’)
2) [Delete element]
YQ [F]
3) [Queue empty?]
If F =R Then
F0
R0
Else FF+1
4) [Return element]
Return (y)
7. For example consider following
operations:
o As shown in figure (a), we insert three elements 5, 10, 15 in simple queue.
o After that we delete 5 and 10 as shown in figure (b). Now even we have a free memory
space we cannot use that memory space. So simple queue results in wastage of memory
space.
o This problem can be solved by using circular queue.
8. Circular Queue:
A circular queue is a queue in which elements are added and removed in a circular
fashion / manner, is called Circular Queue.
:
Represent of circular Queue:
9. o As shown in figure (a), we insert eight elements 10, 20,30,40,50,60,70,80 in simple
queue. After that we delete 10, 20 and 30 as shown in figure (b). Now we have a free
memory space in circular queue and we can use that memory space by incrementing
rear pointer by 1(rear=0).
Operations on Circular Queue
(1) Insert new element in to circular queue
(2) Delete element from circular queue
10. (1) Algorithm to insert element in circular queue
PROCEDURE CQINSERT (Q, F, R, N, Y)
o This function inserts an element in to circular queue.
o The Queue is represented by vector Q which contains N elements.
o F is a pointer which points to the front end
o R is a pointer which points to the rear end
o Y is the element to be inserted.
1) [Reset rear pointer?]
If R = N then
R 1
Else
RR + 1
2) [Overflow?]
if F= R then
Write (“queue overflow”)
Return
3) [Insert element]
Q[R]Y
11. 4) [Is front pointer properly set?]
If F = 0 then
F1
Return
12. (2) Algorithm to delete element from circular queue.
PROCEDURE CQDELETE (Q, F, R,N)
o This function deletes an element from circular queue
o The Queue is represented by vector Q which contains N elements.
o F is a pointer which points to the front end
o R is a pointer which points to the rear end
o Y is temporary variable.
1) [Underflow]
If F = 0 then
Write (Underflow“)
2) [Delete element]
YQ[F]
3) [Queue empty?}
f F = R then
FßRß0
Return(Y)
14. Dequeue:
Dequeue is a linier list in which insertion and deletion operation are performed at
either end of the queue.
Dequeue is also known double ended Queue…
Dequeue is shown below:
15. Operation on Dequeue
(1) Insert an element in to Dequeue
(2) Delete an element from Dequeue
There are two types of Dequeue:
o Input restricted Dequeue: in input restricted Dequeue insertion operation is
performed at only one end of the queue.
o Output restricted Dequeue: in output restricted Dequeue deletion operation is
performed at only one end of the queue.
16. 1.Insert operation in Dequeue
qinsert_beg(q,val)
1. If ( q Rear = Max-1 AND q FRONT=0]
Print “overflow Queue is full” and goto step 4
End If
2. q Front=-1
set q Front=q Rear=0
Set qDqueue [q Front]= val
End If
3. If qRear != MAX=-1
Set num_item=qRear-qFRONT +1
Set i=qRear+1
set j=1
while j<=num_item
Set q Dque[i]=q Dque[i-1]
Set i=i-1
17. Set j=j+1
End While
Set qDque[i]=val
Set qFront=i
Set qRear =qRear +1
Else
Set qFront=qFront-1
Set qDque[qFront]=val
End if
4. End
2.Insert operation At the end of queue
Procedure qinsert_end(q,val)
1. If (qRear =Max-1 And qFront =0)
print “overflow” goto step4
End If
18. 2. If qFront=-1
set q Front=q Rear=0
Set qDqueue [q Front]= val and goto step4
End If
3. If q Rear = MAX-1
Set i=q Front-1
While i< q Rear
Set q Dque[i] =qDque[i+1]
Set i= i+1
End while
Set q Dque[qRear]=val
Set q Front=qFront-1
Else
Set q Rear=qRear+1
Set q Dque[qRear]=val
End IF
4. End
19. 3.Delete operation in the beginning position
Procedure Qdelete_beg(q)
1. If q Front = -1
Print “Underflow”
Return 0 and go to step 5
End If
2. Set del_val=q Dque[qFront]
3.. If q Front =q Rear
Set q front = q Rear = -1
Else
Set qFront =q front +1
End if
4. Return del_val
5. End
20. 4.Delete operation At end of queue
Procedure delete_end(q)
1. If q Front = -1
Print “Underflow”
Return 0 and go to step 5
End if
2. Set q del_val=q Dque[qrear]
3. q front = q Rear
Set qfront = q Rear -1
Else
set q Rear = qrear-1
If qrear = -1
set qfront = -1
End if
End if
4. Return del_val
5.End
21. Priority Queue:
A queue in which we are able to insert items or remove items from any position
based on some priority is known as priority queue.
R1 R2 …… Ri-1 O1 O2 ……. Oj-1 B1 B2 …… Bk-1
1 2 …… 1 2 2 …….. 2 3 3 …… 3
:
o Figure represents a priority queue of jobs waiting to use a computer
o Priorities of 1, 2, 3 have been attached to jobs of real time, on-line and batch
respectively.
o Figure shows how the single priority queue can be visualized as three separate
queues.
22. o When elements are inserted, they are always added at the end of one of the
queues as determined by the priorities
o Elements in the second queue are deleted only when the first queue is empty.
o Elements in the third queue are deleted only when the first and second queue are
empty.
Operation on Priority Queue
1) Insert operation in priority Queue
2) Delete operation in priority Queue
23. 1.Insert operation into priority Queue
Procedure Qinsert (q,val,prno) //prno is the priority of val
1). (qRear[prno]=MAX-1 AND qFront[prno]=0) OR
(qRear[prno]+1=qFront[prno]=0)
Print(“overflow queue”) and goto step-5
End If
2). If qRear[prno-1]=Max-1
set qRear[prno-1]= 0
Else
Set qRear[prno-1]=qRear[prno-1]+1
End if
3). Set qCqueue[prno-1] [qRear[prno-1]] =val.
4). If qFront[prno-1]= -1
Set qFront[prno-1] = 0
End if
5). End
24. 2.delete operation on priority Queue
Procedure Qdelete(q)
1). Set flag=0,i=0
2). while I =MAX-1
If NOT(q Front[prno])= -1
Set flag=1
Set del_val=q Cqueue[1][q Front[i]]
If qFront[ i ]=qRear[i]
Set q Front[i] = qRear[i]=-1
Else If q Rear[prno-1]=Max-1
Set q Rear[prno-1]= 0
Else
Set q Rear[prno-1]=q Rear[prno-1]+1
End if
End if
Break //jump out from while loop
End if
Set i= i+1
End While