DS - UNIT - IV Queues
DS - UNIT - IV Queues
Queues
Learning Material
Queue is a linear Data structure.
Definition: Queue is a collection of homogeneous data elements, where insertion and deletion
operations are performed at two extreme ends.
The insertion operation in Queue is termed as ENQUEUE.
The deletion operation in Queue is termed as DEQUEUE.
An element present in queue is termed as ITEM.
The number of elements that a queue can accommodate is termed as LENGTH of the Queue.
In the Queue the ENQUEUE (insertion) operation is performed at REAR end and DEQUEUE
(deletion) operation is performed at FRONT end.
Queue follows FIFO principle. i.e. First In First Out principle. i.e. a item First inserted into
Queue, that item only First deleted from Queue, so queue follows FIFO principle.
. . - - - . . .
DEQUEUE ENQUEUE
FRONT REAR
Schematic Representation of Queue
Representation of Queue
N-1
1 2 3
N
. . - - - . .
FRONT REAR
Operation on Queue
1. ENQUEUE : To insert element in to Queue
2. DEQUEUE : To delete element from Queue
3. Status : To know present status of the Queue
Algorithm Enqueue(item)
Input: item is new item insert in to queue at rear end.
Output: Insertion of new item queue at rear end if queue is not full.
1. if(rear = = N-1)
a) print "queue is full, not possible for enqueue operation"
2. else
a) if(front = = -1 && rear = = -1) /* Queue is Empty */
i) rear = rear+1
ii) Q[rear] = item
iii) front = 0
b) else
i) rear = rear+1
ii) Q[rear] = item
c) end if
3. end if
Algorithm Dequeue( )
Input: Queue with some elements.
Output: Element is deleted from queue at front end if queue is not empty.
1. if(front = = -1 && rear = = -1)
a) print "Q is empty, not possible for dequeue operation"
2. else
a) if(front = = rear) /* Q has only one element */
i) item = Q[front]
ii) front = -1
iii) rear = -1
b) else
i) item = Q[front]
ii) front = front+1
c) end if
d) print "deleted item is" item
3. end if
End Dequeue
Algorithm Queue_Status( )
Input: Queue with some elements.
Output: Status of the queue. i.e. Q is empty or not, Q is full or not, Element at front end and rear end.
1. if(front = = -1 && rear = = -1)
a) print "Queue is empty"
2. else if (rear = = size-1 )
a) print "Queue is full"
3. else
X X
Front Rear
Linked List Representation of Queue
In Linked List Representation of Queue, Front always points to First node in the Linked List
and Rear always points to Last node in the Linked List.
X X
1
2
Front Rear NULL
3
Before ENQUEUE Rear
header N1 N2 N3 new
X X
Front Rear
After ENQUEUE
Algorithm Dequeue_LL( )
Input: Queue with some elements
Output: Element is deleted at front end, if queue is not empty.
1. if(front = = NULL && rear = = NULL)
a) print "queue is empty, not possible to perform dequeue operation"
2. else
a) if(front = = rear) /* Q has only one element */
i) headerlink = NULL
ii) item = frontdata
iii) front = NULL
iv) rear = NULL
b) else /* Q has more than one element */
i) headerlink = frontlink /* 1 */
ii) item = frontdata
iii) free(front)
iv) front = headerlink /* 2 */
c) end if
d) print "deleted element is item"
3. end if
End_Dequeue_LL
header N1 N2 N3 N4
X X
2
Front Rear
Front
Before DEQUEUE
header N2 N3 N4
X X
Front
Rear
After DEQUEUE
1. Link part of the header node is replaced with address of second node. i.e. address of second
node is available in link part of first node.
2. Front is set to first node in the list.
Algorithm Queue_Status_LL
Input: Queue with some elements
Output: Status of the queue. i.e. Q is empty or not, Q is full or not, Element at front end and rear end.
1. if(front = = NULL && rear = = NULL)
a) print "Q is empty"
2. else if(front = = rear)
a) print "Q has only one item"
3. else
a) print "element at front end is" frontdata
b) print "element at rear end is" reardata
4. end if
End Queue_Status_LL
Front Rear
n-1
n
1
2
j
1 j i n i
Rear
Circular Queue (Logical)
Algorithm CQ_Dequeue( )
Input: Circular Queue with some elements.
Output: Element is deleted from circular queue at front end if circular queue is not empty.
1. if(FRONT= =0 and REAR= =0)
a) print(“Circular Queue is empty, dequeue operation is not possible”)
2. else
i) if(FRONT = =REAR) /* Q has only one element */
a) item=CQ[FRONT]
b) FRONT=0
c) REAR=0
ii) else
a) item=CQ[FRONT]
b) FRONT=(FRONT % N)+1
iii) End if
iv) print(“deleted item is ‘item’ “)
3. end if
End CQ_Dequeue
Deque structure is general representation of both queue and stack. That means the deque can be
used as a queue as well as a stack.
We can implement deque by using double linked list or by circular array (as in circular queue).
Operations on deque:
1. Push_DQ(ITEM): To insert ITEM at the FRONT end of a deque.
2. Pop_DQ(): To remove the FRONT ITEM of a deque.
3. Inject(ITEM): To insert ITEM at the REAR end of a deque.
4. Eject(): To remove the REAR ITEM of a deque.
Operations of deque based on circular array of size LENGTH. Let the array be
DQ[1……LENGTH].
Ahead=LENGTH;
Ahead=1;
REAR=1;
Else
Ahead=1;
Else
If(Ahead==REAR) then
Exit;
Else
FRONT=Ahead;
Stop;
Input: A DQ with elements. Two pointers FRONT and REAR are known.
Exit;
Else
ITEM=DQ[FRONT]
FRONT=0;
REAR=0;
Else
End if;
End if;
Stop;
FRONT=1;
REAR=1;
DQ[REAR]=ITEM;
REAR=Next;
DQ[REAR]=ITEM;
Else
End if;
End if;
Stop;
Input: A DQ with elements. Two pointers FRONT and REAR are known.
Exit;
Else
ITEM=DQ[REAR]
FRONT=0;
Else
ITEM=DQ[REAR];
REAR=LENGTH;
Else
ITEM=DQ[REAR];
REAR=1;
Types of Deque
Deletion
Deletion Insertion
FRONT REAR
Input restricted Deque
2. Output restricted Deque
Here Deque allows deletion at one end (say FRONT end) only, but allows insertion at
both ends.
Insertion
Deletion Insertion
FRONT REAR
Output restricted DEQue