Unit3 Queue 2024
Unit3 Queue 2024
March 3, 2025
• Insertion: ENQUEUE
• Deletion: DEQUEUE
Representation of Queues
• Using an array
• Using single linked list
Algorithm ENQUEUE(ITEM)
Input: An element ITEM that has to be inserted
Output: The ITEM is at the REAR of the queue
1 If (REAR=N-1) then
1 Print “Queue is full”
2 Exit
2 Else
1 If( REAR=-1) and (FRONT=-1) //Queue is empty
1 FRONT = 0
2 EndIf
3 REAR=REAR+1
4 Q[REAR]=ITEM
3 EndIf
4 Stop
1 If (Front=-1) then
1 Print “ Queue is empty”
2 Exit
2 Else
1 ITEM=Q[FRONT] //Get the element
2 IF (FRONT=REAR) // When Queue contains single element
1 REAR=-1 // The queue becomes empty
2 FRONT = -1
3 Else
1 1. FRONT=FRONT+1
4 EndIf
3 EndIf
4 Stop
• In a linked queue, each node consists of two fields, data field and
reference field
• Containing two pointers
1 Front
2 Rear
• Front: point to the head of the node
• Rear: point to the end of the list (last node).
• Enque operates upon the rear pointer
• Deque operates upon the front pointer
1 Circular Queue
2 Priority Queue
3 Dequeue
• The worse case time of push and pop operation become O(1) if array
positions are arranged in circular way
• Mapping function:
location(i) = (location(front element) + i)% arraylength
• location(front) = 3
• The rear of the queue is at index 1
cases of queue
• Circular Queue is empty
• FRONT = −1 (if index from 0)
• REAR = −1
• Queue is full
• (REAR + 1)% arrayLength = = FRONT
1 if (front == -1)
1 print ”Queue is empty”
2 Exit
2 Else
1 ITEM = CQueue[front] // Retrieve the element at the front
2 if (front == rear) // If the queue has only one element
• front = -1
• rear = -1
3 front = (front + 1) % arrayLength // Move front one position ahead,
circularly
4 EndIf
3 EndIf
4 Return ITEM
1 if (front == -1)
1 print ”Queue is empty”
2 Exit
2 Else
1 ITEM = CQueue[front] // Retrieve the element at the front
2 if (front == rear) // If the queue has only one element
• front = -1
• rear = -1
3 front = (front + 1) % arrayLength // Move front one position ahead,
circularly
4 EndIf
3 EndIf
4 Return ITEM
Enqueue and dequeue operations in a circular queue have a time
complexity of O(1)
3 Exit
• Traffic Management
• Operating System Processes
• Data compression
• Dijkstra’s Algorithm