Queue
Queue
MULLANA (AMBALA)
Programme: BCA
Course: BCA-202: Data Structure
QUEUE:
Queue is also a linear data structure, just like stack data structure, in which the first element is inserted from
one end called the REAR(also called tail), and the removal of existing element takes place from the other end
called as FRONT(also called head).
This makes queue as FIFO(First in First Out) data structure, which means that element inserted first will be
removed first.
Which is exactly how queue system works in real world. If you go to a ticket counter to buy movie tickets, and
are first in the queue, then you will be the first one to get the tickets. Right? Same is the case with Queue data
structure. Data inserted first, will leave the queue first.
The process to add an element into queue is called Enqueue and the process of removal of an element from
queue is called Dequeue.
1. Like stack, queue is also an ordered list of elements of similar data types.
2. Queue is a FIFO( First in First Out ) structure.
3. Once a new element is inserted into the Queue, all the elements inserted before the new element in
the queue must be removed, to remove the new element.
Page 1
MAHARISHI MARKANDESHWAR (DEEMED TO BE UNIVERSITY),
MULLANA (AMBALA)
Programme: BCA
Course: BCA-202: Data Structure
Applications of Queue
Queue, as the name suggests is used whenever we need to manage any group of objects in an order in which the
first one coming in, also gets out first while the others wait for their turn, like in the following scenarios:
1. Serving requests on a single shared resource, like a printer, CPU task scheduling etc.
2. In real life scenario, Call Center phone systems uses Queues to hold people calling them in an
order, until a service representative is free.
3. Handling of interrupts in real-time systems. The interrupts are handled in the same order as they
arrive i.e First come first served.
Page 2
MAHARISHI MARKANDESHWAR (DEEMED TO BE UNIVERSITY),
MULLANA (AMBALA)
Programme: BCA
Course: BCA-202: Data Structure
Whenever we move head one position ahead, after removal of first element, the size on Queue is reduced by
one space each time.
QINSERT( QUEUE, SIZE, FRONT, REAR, ITEM)
1. IF REAR =SIZE, then:
WRITE: OVERFLOW and Return
2. IF FRONT =NULL, then:
Set FRONT:=1 and REAR:=1
ELSE:
Set REAR:=REAR+1
[End of IF Structure]
3. Set QUEUE[REAR] := ITEM
4. Return
Page 3
MAHARISHI MARKANDESHWAR (DEEMED TO BE UNIVERSITY),
MULLANA (AMBALA)
Programme: BCA
Course: BCA-202: Data Structure
ELSE:
Set FRONT:=FRONT+1
[End of IF Structure]
4. Return
1 2 3 4 5
30 40
Front=4
Rear=5
Page 4
MAHARISHI MARKANDESHWAR (DEEMED TO BE UNIVERSITY),
MULLANA (AMBALA)
Programme: BCA
Course: BCA-202: Data Structure
Circular Queue
ELSE:
Set REAR:=REAR+1
[End of IF Structure]
3. Set QUEUE[REAR] := ITEM
Return
Page 5
MAHARISHI MARKANDESHWAR (DEEMED TO BE UNIVERSITY),
MULLANA (AMBALA)
Programme: BCA
Course: BCA-202: Data Structure
1 2 3 4 5
70 80 90 100
Front=2
Rear=5
DEQUE
Types of Deque:
1. Input Restricted
2. Output Restricted
Page 6
MAHARISHI MARKANDESHWAR (DEEMED TO BE UNIVERSITY),
MULLANA (AMBALA)
Programme: BCA
Course: BCA-202: Data Structure
Input Restricted:-
1 2 3 4 5
10 20 30
Rear
40
Front=2
Rear=4
Dequeue_Insert (Q,REAR,N,FRONT,ITEM)
Page 7
MAHARISHI MARKANDESHWAR (DEEMED TO BE UNIVERSITY),
MULLANA (AMBALA)
Programme: BCA
Course: BCA-202: Data Structure
1 2 3 4 5
30
Dequeue_Delete (Q,REAR,N,FRONT,ITEM)
5. IF(FRONT=REAR)
Page 8
MAHARISHI MARKANDESHWAR (DEEMED TO BE UNIVERSITY),
MULLANA (AMBALA)
Programme: BCA
Course: BCA-202: Data Structure
Priority Queue
1 2 3 4 5
10
20 30 40 5
15
70 50 60
80
Front Rear
2 2
1 3
0 0
4 1
3 3
Page 9
MAHARISHI MARKANDESHWAR (DEEMED TO BE UNIVERSITY),
MULLANA (AMBALA)
Programme: BCA
Course: BCA-202: Data Structure
Page 10