Chapter 1

Data structures and cost conventions

First identify the required operation and the information already supplied. Access by index, search by key and access through a node pointer are different tasks. Storage layout determines which steps are constant time.

1.1 Abstract operations and representation

Past papers · CS 2014–2026 (23 papers): 1 question, 2 marks · DA 2024–2026 (3 papers): 1 question, 1 mark

An abstract data type specifies operations and their behaviour. A representation supplies memory layout and algorithms: a stack can use an array or a linked list, for example. A sequence has an order; a set has distinct members; a dictionary associates keys with values. A priority queue removes an extreme key, whereas an ordinary queue removes the earliest outstanding arrival.

RepresentationAccess item at indexFind a specified keyChange local links or a known cell
Contiguous array, nn itemsΘ(1)\Theta(1)Θ(n)\Theta(n) worst case without order/indexΘ(1)\Theta(1) overwrite; insertion may shift Θ(n)\Theta(n) items
Singly linked list, head onlyΘ(n)\Theta(n) worst caseΘ(n)\Theta(n) worst caseΘ(1)\Theta(1) if all required adjacent nodes are known
Doubly linked listΘ(n)\Theta(n) worst caseΘ(n)\Theta(n) worst caseΘ(1)\Theta(1) unlink given a node, with head/tail updates

Here nn is the number of stored items. A bound assumes fixed-size keys, values and machine-word pointer operations. Comparing long strings or copying large records has an additional cost. Auxiliary space excludes the input and required output unless the question says otherwise.

1.2 Operation sequences and amortised costs

Worst case is the largest cost of one operation or one entire specified sequence. Expected cost uses a stated probability model. Amortised cost bounds an entire sequence without assuming random inputs. OO is an upper bound, Ω\Omega a lower bound and Θ\Theta a tight bound; constants and sufficiently small inputs do not determine asymptotic growth.

For an array that doubles capacity when full, copied item counts form 1+2+4+⋯1+2+4+\cdots. Over n≥1n\ge1 appends, fewer than 2n2n old items are copied and nn new items written: total less than 3n3n item writes. Each append is therefore amortised Θ(1)\Theta(1) although a resizing append costs Θ(n)\Theta(n). This assumes unit-cost item copies and geometric capacity growth; fixed-increment growth does not give the same bound.

Last-minute recall

  • State what one primitive step means.
  • A pointer to a node is not a pointer to its predecessor.
  • Separate a key search from a known-location update.
  • Multiply costs by operation counts; use changing sizes when necessary.
  • Amortised constant time allows occasional linear-time operations.
  • Arrays support indexing; linked lists follow links.

Revise offline: the whole Programming and Data Structures handbook as one PDF

55 pages · 2.6 MBFree to read online. Sign in to download the PDF.We save the handbook, version and request time in My downloads. Privacy policy

All chapters · Corrections