Show understanding that a stack, queue and linked list are examples of ADTs

Published by Patrick Mutisya · 14 days ago

Cambridge A-Level Computer Science 9618 – Introduction to Abstract Data Types (ADT)

10.4 Introduction to Abstract Data Types (ADT)

An Abstract Data Type (ADT) defines a data structure by specifying the operations that can be performed on it and the mathematical properties of those operations, without prescribing how the structure is implemented. The focus is on what the data type does, not how it does it.

Key Characteristics of an ADT

  • Encapsulation of data and operations.
  • Well‑defined interface (set of operations).
  • Implementation independence – the same ADT can be realised with arrays, linked structures, or other mechanisms.
  • Behaviour described by axioms or contracts (e.g., LIFO for a stack).

Common ADTs in the A‑Level Syllabus

The following three structures are classic examples of ADTs that you will encounter:

  1. Stack
  2. Queue
  3. Linked List

1. Stack ADT

A stack follows the Last‑In‑First‑Out (LIFO) principle. The abstract operations are:

  • push(x) – insert element x on the top.
  • pop() – remove and return the top element.
  • top() – return the top element without removing it.
  • isEmpty() – true if the stack contains no elements.

Mathematically, if \$S\$ denotes the current sequence of elements (top on the right), then:

\$\text{push}(S, x) = S \cdot x\$

\$\$\text{pop}(S) = \begin{cases}

(S', x) & \text{if } S = S' \cdot x \\

\text{error} & \text{if } S = \varepsilon

\end{cases}\$\$

Suggested diagram: A vertical stack showing elements with the top at the uppermost position.

2. Queue ADT

A queue follows the First‑In‑First‑Out (FIFO) principle. The abstract operations are:

  • enqueue(x) – insert element x at the rear.
  • dequeue() – remove and return the front element.
  • front() – return the front element without removing it.
  • isEmpty() – true if the queue contains no elements.

If \$Q = \langle q1, q2, \dots , q_n\rangle\$ denotes the ordered sequence (front on the left), then:

\$\text{enqueue}(Q, x) = \langle q1, q2, \dots , q_n, x\rangle\$

\$\$\text{dequeue}(Q) = \begin{cases}

( \langle q2, \dots , qn\rangle , q_1) & \text{if } n \ge 1 \\

\text{error} & \text{if } n = 0

\end{cases}\$\$

Suggested diagram: A horizontal line with arrows indicating entry at the rear and exit at the front.

3. Linked List ADT

A linked list is a collection of nodes where each node contains data and a reference (link) to the next node. The abstract operations typically include:

  • insert(i, x) – insert element x at position i (0‑based).
  • delete(i) – remove the element at position i.
  • get(i) – retrieve the element at position i.
  • size() – return the number of elements.
  • isEmpty() – true if the list contains no elements.

The ADT does not prescribe whether the list is singly‑linked, doubly‑linked, or circular; those are implementation choices.

Suggested diagram: Nodes represented as boxes with arrows pointing to the next node, illustrating a singly‑linked list.

Comparison of the Three ADTs

PropertyStackQueueLinked List
Access OrderLIFO – last element added is first removedFIFO – first element added is first removedSequential – any position can be accessed (subject to traversal cost)
Typical Operationspush, pop, top, isEmptyenqueue, dequeue, front, isEmptyinsert, delete, get, size, isEmpty
Common ImplementationsArray (fixed size) or linked listArray (circular buffer) or linked listSingly‑linked, doubly‑linked, circular
Complexity (average case)push/pop – \$O(1)\$enqueue/dequeue – \$O(1)\$insert/delete at head – \$O(1)\$; at arbitrary position – \$O(n)\$

Why Treat These Structures as ADTs?

By describing a stack, queue, or linked list as an ADT, we can:

  • Reason about program correctness using the abstract properties (e.g., LIFO for a stack).
  • Swap implementations without altering client code – a stack implemented with an array can be replaced by a linked‑list version.
  • Apply generic algorithms that operate on any ADT that satisfies a given contract.

Understanding the distinction between an ADT and its concrete implementation is essential for writing modular, maintainable code and for performing the algorithmic analysis required at A‑Level.