← Course home

Linear structures · Lesson 17 of 52

Queue and deque operations

Compare FIFO and double-ended operations with linked and circular-buffer invariants.

Full lessonAbout 10 min

You will learn to trace FIFO order and compare linked and circular queue invariants.

Recommended first: Arrays and Python sequence costs, Singly linked list.

On this page 6 sections

The idea

Enqueue a, b, and c. The first dequeue returns a.

A queue is first-in, first-out. enqueue adds at the back and dequeue removes from the front. The linked queue in this repository uses a doubly linked list with a cached tail, so both ends are Θ(1). A ring buffer stores the same order in an array with a head index and a tail index. A deque allows add and remove at both ends. The order promise is the part to learn first. After enqueue a, b, c, two dequeues return a and then b. A Python list can act as a stack with append and pop at the end, and that is a different order from this queue.

A worked trace

StepStateWhat changed
Enqueuefront = aEnqueued a. The front is a.
Enqueuefront = aEnqueued b. The front is a.
Enqueuefront = aEnqueued c. The front is a.
Dequeuefront = bThe first dequeue returns a. The front is now b.

Why it is correct

The front is the earliest item that has not been dequeued. Enqueue does not change the front unless the queue was empty, in which case the new item is both front and back. Dequeue advances the front to the next node and returns the old one. That is why the exit order matches the arrival order. A stack would have returned c first. Mixing the two is the usual bug when a graph search asks for a queue. The code uses a list with pop() at the end and insert at the end.

A common mistake

Using pop at the end of a list returns the latest item and breaks the queue order.

What it costs

Linked enqueue and dequeue are Θ(1) time and one new node of memory. A circular array is Θ(1) at both ends until it resizes, and then one resize copies the live items. n operations are Θ(n) total either way. Indexing the middle of a linked queue is Θ(n). If the algorithm needs the middle, an array is the better store.

Breadth-first search is the payoff. It is correct only when the frontier removes the oldest vertex. A list used as a stack visits a different order and the distance claim fails. The queue lesson is the reason that search works.

Run it

python
from bitsandbytes.queues.linked_queue import LinkedQueuequeue = LinkedQueue()queue.enqueue('a')queue.enqueue('b')print(queue.dequeue())# prints: a 

Practice and next step

Open Comparison sorts next. Comparison sorts use an ordered prefix. Insertion sort is the one this lesson traces.

Implementation and tests Optional reference

Library code 3

  • bitsandbytes.queues.linked_queue
  • bitsandbytes.queues.circular_queue
  • bitsandbytes.deques.linked_deque

Use these references to compare the explanation with repository code and tests. A test link means it imports a lesson symbol; it does not by itself claim complete behavior coverage.

Step-by-step trace

Enqueue a, b, c

The left end is the front. Dequeue removes from the left.

front
a

State

  1. 0acomparing

Enqueued a. The front is a.

Practice feedback is loading. The lesson explanation remains available above.