liveBook · Manning

Queue

As explained in Get Programming: Learn to code with Python

A queue is a data structure in which elements are added at one end and removed from the other, so the earliest added element is removed first.

A queue is a line of elements: new items join at the end, and the item waiting longest leaves first. This is called first-in-first-out (FIFO). A list can provide the underlying storage while queue operations give it more specific behavior. section 26.4

A grocery-store line in Python

Start with line = []. Append Ana and Bob, then pop(0) to remove Ana. Append Claire and Dave; successive pop(0) operations remove Bob, Claire, and Dave in that order. The first arrival is therefore the first departure. section 26.4

Listing 26.5. A queue of people represented by a list
line = []                      #1
line.append('Ana')             #2
line.append('Bob')             #3
line.pop(0)                    #4
line.append('Claire')          #5
line.append('Dave')            #5
line.pop(0)                    #6
line.pop(0)                    #6
line.pop(0)                    #6

Follow the queue operations line by line: append adds people to the end, while pop(0) removes the person at the front. section 26.4

To turn the idea into a reusable queue object, decide:

  • Which data structure represents the queue. section 32.3
  • Which methods add one or many items, remove one or many items, report size, and display the queue. section 32.3
  • How objects are created and used through a sequence of operations. section 32.3

How Get Programming: Learn to code with Python covers it

How deep it goes
Defines FIFO behavior, demonstrates list operations, and extends the idea to designing a queue class.
Examples
Uses a grocery-store line and a Python list containing Ana, Bob, Claire, and Dave.
What it assumes
Assumes basic list operations and teaches queue behavior through append and pop operations.
What you can do after
Build it from scratch by implementing a queue class with add, remove, size, display, and batch-operation methods.
How it connects
Connects queues with lists, stacks, classes, objects, methods, and FIFO ordering.

Also explained in

Compare how 23 books explain queue →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Get Programming: Learn to code with Python. The section links point to the parts of the book it draws on.