Skip to content
Part IA Lent Term

Queues

Abstract Data Type

A queue is a first-in, first-out (FIFO) collection. Elements are added at the rear and removed from the front. Operations:

OperationDescription
ENQUEUE(Q, x)Add element xx to the rear of the queue
DEQUEUE(Q)Remove and return the front element
FRONT(Q)Return the front element without removing it
IS-EMPTY(Q)Return true iff queue contains no elements

Circular Array Implementation

A fixed-capacity array Q[1..n] with two indices: head (front of queue) and tail (first free slot after the rear). Both wrap around modulo nn using an inc helper:

inc(x): return (x == Q.length) ? 1 : x + 1
  • Initially: head = tail = 1 (empty queue).
  • Enqueue: store at Q[tail], then tail = inc(tail).
  • Dequeue: retrieve Q[head], then head = inc(head).
  • Empty: head == tail.
  • Full: inc(tail) == head (or equivalently head == tail + 1 modulo nn).

All operations are Θ(1)\Theta(1). At most n1n-1 items can be stored (one slot is sacrificed to distinguish full from empty, since both would have head == tail).

Circular queue diagram

Linked List Implementation

Maintain head (front) and tail (rear) pointers. Enqueue adds at the tail, dequeue removes from the head. Both operations are Θ(1)\Theta(1). No capacity limit.

ENQUEUE(Q, x):
    node = new Node(value=x, next=NIL)
    if Q.tail == NIL:
        Q.head = Q.tail = node
    else:
        Q.tail.next = node
        Q.tail = node

DEQUEUE(Q):
    if Q.head == NIL: error "empty"
    x = Q.head.value
    Q.head = Q.head.next
    if Q.head == NIL: Q.tail = NIL
    return x

Circular Buffer Variant

The circular array queue is also known as a circular buffer. It is widely used in operating systems for kernel pipes and I/O buffering. It avoids the allocation/deallocation overhead of linked lists and provides cache-friendly linear memory access.

Deque (Double-Ended Queue)

A deque supports insertion and deletion at both ends: PUSH-FRONT, PUSH-BACK, POP-FRONT, POP-BACK. It can be implemented with a doubly linked list or a circular array, all operations Θ(1)\Theta(1). A deque generalises both stacks and queues.

Applications

ApplicationHow the queue is used
Task schedulingFIFO scheduling: tasks processed in arrival order
Breadth-first searchDiscovered vertices are enqueued; processed in FIFO order
Print queuesJobs printed in submission order
Buffering / I/OData buffered between producer and consumer (circular buffer)
Message passingMessages delivered in order between threads/processes

Comparison: Stack vs. Queue

AspectStackQueue
OrderLIFOFIFO
Insert calledPushEnqueue
Remove calledPopDequeue
Array implementationOne index (top)Two indices (head, tail) with wrap
Full detectiontop == ninc(tail) == head
ApplicationsRecursion, undo, parsingScheduling, BFS, buffering

Summary

PropertyCircular ArrayLinked List
ENQUEUEΘ(1)\Theta(1)Θ(1)\Theta(1)
DEQUEUEΘ(1)\Theta(1)Θ(1)\Theta(1)
FRONTΘ(1)\Theta(1)Θ(1)\Theta(1)
IS-EMPTYΘ(1)\Theta(1)Θ(1)\Theta(1)
Max capacityn1n-1 itemsUnbounded
SpaceΘ(n)\Theta(n) pre-allocatedPer-element
PrincipleFIFO