Programming Search⌘K

Stacks, queues, trees and graphs

The other standard collections, as programmers use them from libraries: stacks, queues, deques, priority queues, ring buffers, trees and graphs.

Concepts

  • Queue

    A queue is a collection that gives back its items in the order they arrived: the first item in is the first item out. Programs use queues for work that waits its turn.

    intro
  • Stack

    A stack is a collection that gives back its items in reverse order: the last item pushed is the first item popped. Most languages use a growable array or a library type as a stack.

    intro
  • Tree

    A tree is a set of nodes where each node has one parent, except the root, and any number of children. File systems, the DOM, syntax trees and menus are trees.

    intro
  • Deque

    A deque, or double-ended queue, adds and removes items at both ends. It can serve as a stack and as a queue, and most libraries build it on a ring buffer or on linked blocks.

    core
  • Graph

    A graph is a set of nodes joined by edges, with no root and no limit on links. Programs store it as an adjacency list, a map from each node to its neighbors, or as a matrix.

    core
  • Priority queue

    A priority queue gives back the item with the best priority first, not the oldest item. Libraries build it on a binary heap, so a push or a pop takes logarithmic time.

    core
  • Ring buffer

    A ring buffer is a fixed-size array used as a queue. The read and write positions wrap around to the start, so no item moves, and a full buffer refuses new items or overwrites the oldest.

    core

Tasks

  • Use a queue

    Run jobs in turn with round-robin scheduling, with enqueue and dequeue on the standard queue type of each tier 1 language, and see why list.pop(0) is slow.

    10 languagesintro
  • Use a stack

    Check that the brackets of a text match and nest, with push, pop and peek on the standard stack type of each tier 1 language.

    10 languagesintro
  • Use a priority queue

    Serve requests by priority with the standard priority queue of each tier 1 language, with a custom order, a max-heap and ties in arrival order.

    10 languagescore