StudyHub

Algorithms · Hub

Linear structures

Eight ways to keep items in a line. Arrays give instant access by index; linked lists make relinking cheap; stacks, queues and deques restrict where you add and remove; a priority queue always serves the most urgent item.

Why it matters: the structure you pick decides which operations are $O(1)$ and which are $O(n)$. Choosing well is often a bigger speed-up than any clever algorithm.

5 random questions mixed from all eight structures.

Quick comparison

StructureAccess $i$-thAdd / remove at frontAdd / remove at backDelete a known itemOrder
Array$O(1)$$O(n)$$O(1)$ amortized$O(n)$By index
Singly linked list$O(n)$$O(1)$$O(1)$ add with tail / $O(n)$ remove$O(n)$By links
Doubly linked list$O(n)$$O(1)$$O(1)$$O(1)$By links
Circular linked list$O(n)$$O(1)$$O(1)$ add$O(n)$Wraps around
Stacktop only—$O(1)$ push / pop—LIFO
Queuefront only$O(1)$ dequeue$O(1)$ enqueue—FIFO
Deque$O(1)$ (array)$O(1)$$O(1)$—Both ends
Priority queuemin only$O(\log n)$ extract-min$O(\log n)$ insert—By priority

The eight structures

Array

Contiguous slots, found by address arithmetic.

O(1) access · O(n) insert in the middle

Study

Singly linked list

Nodes chained by next pointers.

O(1) push front · O(n) search

Study

Doubly linked list

Next and prev pointers in every node.

O(1) delete a known node · 2 pointers per node

Study

Circular linked list

The last node points back to the first.

O(1) both ends with a tail · no NULL end

Study

Stack

Push and pop at the top only.

LIFO · O(1) push / pop / peek

Study

Queue

Enqueue at the back, dequeue at the front.

FIFO · O(1) with a circular array

Study

Deque

A queue open at both ends.

O(1) push / pop at both ends

Study

Priority queue

Always serves the smallest key first.

O(log n) insert / extract-min · O(1) peek

Study

Which one to pick?

flowchart TD
  A{"Which item do you remove next?"} -->|"the newest"| S["Stack"]
  A -->|"the oldest"| Q["Queue"]
  A -->|"newest or oldest, both ends"| D["Deque"]
  A -->|"the most urgent"| P["Priority queue"]
  A -->|"any item"| B{"Need access by index?"}
  B -->|yes| AR["Array"]
  B -->|no| C{"Delete nodes you already hold, or walk backwards?"}
  C -->|yes| DL["Doubly linked list"]
  C -->|no| E{"Cycle through items forever?"}
  E -->|yes| CL["Circular linked list"]
  E -->|no| SL["Singly linked list"]
      

Key points

External resources

Videos

Review schedule

Come back after 3, 7, 14, then 30 days. Spacing beats cramming.

Feynman prompt

Explain this topic in your own words, as if teaching a beginner.

Explain it in your own words first, then record a 2–3 minute video on your phone.

25:00

Ready to record? Open your phone camera now and record a 2–3 minute explanation.

My notes

Saved only in this browser (localStorage). Nothing is uploaded.