What is a queue?
• It is an ordered group of homogeneous items of elements.
• Queues have two ends:
▫ Elements are added at one end REAR
▫ Elements are removed from the other end FRONT • The element added first is also removed first
• Insertions are at the rear of the queue and removals are at the front of the queue
• Main queue operations:
▫ enqueue(object): inserts an element at the end of the queue
• Definitions: (provided by the user)
▫ MAX_ITEMS: Max number of items that might be on
▫ ItemType: Data type of the items on the queue
– Boolean IsEmpty – Boolean IsFull
Enqueue (ItemType newItem)
• Function: Adds newItem to the rear of the
• Preconditions: Queue has been initialized and is
Dequeue (ItemType& item)
• Function: Removes front item from queue and
returns it in item.
• Preconditions: Queue has been initialized and is
• Post-conditions: Front element has been
• Implement the queue as a circular structure. • How do we know if a queue is full or empty? • Initialization of front and rear.
• Auxiliary queue operations:
▫ object front(): returns the element at the front without removing it
▫ integer size(): returns the number of elements stored
Operation Output Q
enqueue(5) – (5)
enqueue(3) – (5, 3)
dequeue() 5 (3)
enqueue(7) – (3, 7)
dequeue() 3 (7)
front() 7 (7)
dequeue() 7 ()
dequeue() “error” ()
isEmpty() true ()
enqueue(9) – (9)
enqueue(7) – (9, 7)
size() 2 (9, 7)
enqueue(3) – (9, 7, 3)
enqueue(5) – (9, 7, 3, 5)
Applications of Queues
• Direct applications
▫ Waiting lists, bureaucracy
▫ Access to shared resources (e.g., printer) ▫ Multiprogramming
• Indirect applications
Representation of Queues
• Represented in two ways:
or Linear Arrays
• Two pointers variables: FRONT, containing the location of the front element of the queue and REAR, containing the location of the rear element of the queue.
• Whenever an element is deleted from the queue, the value of FRONT is increased by 1; this can be implemented by the assignment
• Whenever an element is added to the queue, the value of REAR is increased by 1; this can be implemented by the assignment:
• When a new item is inserted at the rear, the pointer to rear moves upwards.
• Similarly, when an item is deleted from the queue the front arrow moves downwards.
• To solve this problem, queues implement wrapping around. Such queues are called Circular Queues.
• Both the front and the rear pointers wrap around to the beginning of the array.
• It is also called as “Ring buffer”.
• Generally it is assumed that array QUEUE is circular, i.e. QUEUE comes after QUEUE[N].
• With this assumption, we insert ITEM into the queue by assigning ITEM to QUEUE.
• Instead of increasing REAR to N+1, we reset REAR to 1 and then assign
• Similarly if FRONT=N and an element of QUEUE is deleted, we reset FRONT=1 instead of increasing FRONT to N+1
• If queue contains only one element, i.e.
• And if that only element is deleted, then
1. [Queue already filled?]
if FRONT=1 and REAR=N or if FRONT=REAR+1, then:
Write: Overflow and Return. 2. [Find new value of REAR]
If FRONT:=NULL, then : [Queue initially empty] Set FRONT:=1 and REAR:=1
Else if REAR:=N, then Set REAR:=1
Else: Set REAR:=REAR+1.
3. Set QUEUE[REAR]:=ITEM [inserts new element]
1. [Queue already empty?]
if FRONT:=NULL, then:
Write: UNDERFLOW and Return. 2. Set ITEM:=QUEUE[FRONT]
3. [Find new value of FRONT]
If FRONT:=REAR, then : [Queue has only one element to start]
Set FRONT:=NULL and REAR:=NULL Else if FRONT:=N, then
Else: Set FRONT:=FRONT+1.
Linked Representation of QUEUE
• Two pointer variables: FRONT and REAR.
• INFO fields of the list hold the elements of the
queue and LINK field holds pointer to the neighboring elements in the queue.
1. [Available space?] if AVAIL=NULL, then write OVERFLOW and exit.
2. [Remove first node from AVAIL list]
Set NEW:=AVAIL and AVAIL:=LINK[AVAIL] 3. Set INFO[NEW]:=ITEM and
4. If (FRONT=NULL) then FRONT=REAR=NEW [if Q is empty then ITEM is the first element in
Else set LINK[REAR]:=NEW and REAR=NEW [Rear points to the new node appended to the
1. [Linked queue empty?] if (FRONT=NULL) then write: UNDERFLOW and EXIT.
2. Set TEMP:=FRONT [If linked queue is
nonempty, remember FRONT in a temporary variable TEMP].
3. ITEM=INFO[TEMP] 4. FRONT=LINK[TEMP]
5. LINK[TEMP]=AVAIL and AVAIL=TEMP
• It is a double-ended queue.
• Items can be inserted and deleted from either ends.
• More versatile data structure than stack or queue.
• There are two pointers: LEFT or RIGHT.
• Two variations of a DEQUE: input restricted
• More specialized data structure.
• Similar to Queue, having front and rear. • Items are removed from the front.
• Items are ordered by key value or priority so that the item with the lowest key (or highest) is always at the front.
One-way list representation of Priority
• Each node contains three items of information: INFO, PRN(priority number) and link number (LINK)
• A node X precedes Y in the list (1) when X has higher priority than Y
[deletes and processes the first element
in a priority queue]
1. Set ITEM:=INFO[START]
[saves the data in the first node.] 2. Delete first node from the list 3. Process ITEM.
[Adds an ITEM with priority number N
to a priority queue]
1. Traverse the one-way list until finding a node X whose priority number exceeds N. Insert ITEM in front of node X.
Array representation of a priority
[Algorithm deletes and processes the first element in a priority queue]
1. [Find the first non-empty queue.]
Find the smallest K such that FRONT[K]≠NULL. 2. Delete and process the FRONT element in row
[Algorithm adds an ITEM with priority number M to a priority queue]
1. Insert ITEM as the rear element in row M of Queue.