Module 10 of 39

Arrays, linked lists, stacks, and queues

Compare arrays, matrices, stacks, queues, linked lists, and rooted-tree representations by the operations they support.

A collection is useful only if it supports the work we need to do. A reading log may need fast access by position. An undo history needs the most recent action. A waiting line needs the oldest request. These are different promises, even if all three hold a sequence of items.

Background for this page. A reference identifies another node or object. Following references differs from computing an array position. You should be comfortable with lists of values. Related explanations: Binary heaps, priority queues, and heapsort · Binary search trees: search, insertion, and deletion.

An array stores items in a sequence of positions. Given an index, we can reach an item in O(1) time. A fixed-size array has a set capacity. A dynamic array can grow by allocating a larger block and copying items; most appends are cheap, while an occasional growth is expensive. Across many appends, the average cost per append can still be constant in the amortized sense we will study later. Inserting near the beginning may require shifting many items, so it is O(n) in the worst case.

A matrix is an array with rows and columns. In a row-major layout, a cell at row r, column c in a matrix with w columns is at offset r·w + c in the flat storage. That formula explains why we can reach a known cell directly. It also reminds us to check that both indexes are within bounds.

A stack allows adding and removing at one end. The last item added is the first removed: useful for undo actions and nested function calls. A queue removes from the opposite end from where it adds: useful when requests should be processed in arrival order. Both are abstract data types: they specify operations and behavior, not one required memory layout. An array or linked structure can implement either one. Removing from the front of a simple Python list shifts the remaining items; a queue implementation should avoid that cost when frequent removals matter.

In a linked list, each node stores a value and a reference to another node. A singly linked list points forward; a doubly linked list points both ways. If we already hold the right node reference, changing links can insert an item without shifting later values. Finding the position first may still take O(n), and reaching the item at index i requires following links. Deletion also needs enough references to repair the surrounding links. “Linked-list insertion is O(1)” is true only after those conditions have been met.

# Insert new after a node we already hold.
new.next = current.next
# Keep the rest of the list reachable.
current.next = new

Before insertion A points to C and B is separate; afterward the chain is A to B to C

The picture shows why we link the new node to the rest of the list before changing A’s link.

The order of those two assignments matters. If we replace current.next first, we may lose the only path to the rest of the list. For an empty list, or an insertion at the head, we also need to update the head reference. A doubly linked list needs more link changes so both directions stay consistent.

A rooted tree has a root and parent-child relationships. One representation gives each node links to its children; another stores a first-child link and a next-sibling link. Arrays work well for nearly complete trees such as heaps. The shape of a structure helps determine how we represent it, but the operations we need should decide which shape we choose.

To insert B between linked nodes A and C, first set B’s next reference to C, then set A’s next reference to B. Overwriting A’s old link too early can lose access to C. These link changes take constant time when A is already known. Finding A from the head may still take linear time. This condition belongs in any claim that insertion is O(1). Arrays offer direct indexed access but may shift many items when inserting near the front.

Practice. Pick a structure for each task: undo the latest edit, process requests in arrival order, or read the millionth item by index. Explain what operation led to your choice.

Answer. A stack fits undo, a queue fits arrival order, and an array fits direct indexing. A real program may use a standard library implementation of each rather than building it from scratch.

Try another case. How can an array queue remove its first item without shifting all the others?

Reasoning. Keep a front index or use a circular buffer. Advancing the front changes which slot is logically first.

View the full course and choose another topic.

Image preview

Fit