Skip to main content

Linear Data Structures

Linear structures organize elements in a sequence, but their representations and permitted operations differ.

Comparison​

StructureIndexed accessInsert/remove at endInsert/remove at known interior positionMain trade-off
Dynamic arrayO(1)O(1)amortized O(1)O(1)O(n)O(n) shiftslocality and random access
Singly linked listO(n)O(n)O(1)O(1) with tail for insert; tail removal O(n)O(n)O(1)O(1) after predecessor is knownindirection and node overhead
Doubly linked listO(n)O(n)O(1)O(1) with endpointsO(1)O(1) after node is knownextra link per node
Stacknot part of contractpush/pop at topnot allowed by interfaceLIFO discipline
Queuenot part of contractenqueue/dequeue at opposite endsnot allowed by interfaceFIFO discipline

Finding an interior node or position is separate from modifying it. Saying “linked-list insertion is O(1)O(1)” silently assumes the relevant node or predecessor is already available.

Selection rules​

  • Prefer a dynamic array for general-purpose sequences and iteration-heavy work.
  • Prefer linked nodes when stable node identity and local splicing dominate.
  • Expose a stack or queue when restricted access communicates an algorithmic invariant better than a general list.
  • Use a circular buffer for a bounded queue with predictable storage.

In CPython, list uses a dynamic array of object references; this storage layout is implementation-specific. collections.deque supports efficient operations at both ends.

Choose by the next operation​

For retrieving item 100 directly, follow arrays. For splicing next to a node already held by an algorithm, follow linked lists; a numeric position alone does not provide that node. For matching the latest unfinished delimiter, use a stack. For processing arrivals in order, use a queue.

Here nn is the number of stored elements and moving a slot or following a link has constant cost. Amortized cost bounds a whole sequence of operations, including occasional resizes; it is not an average over random inputs. The stack and queue rows describe interfaces, so their implementation costs must be read separately. These comparisons concern in-memory sequential containers, not concurrent message delivery or on-disk indexing.

Source​

Explore connectionsOpen network