Abstract data types: stacks, queues and linked lists
A-Level Computer Science · Algorithms and Data Structures
What an abstract data type is
An ADT is defined by the operations it supports, not by how it is stored. A stack is a stack because of push and pop, whether it is built from an array or a linked list. Separating the two is the idea being examined.
Stack — last in, first out
Operations: push adds to the top, pop removes from the top, peek looks without removing. One pointer, the top, is enough. Stack overflow happens on push when full; stack underflow on pop when empty — both must be checked. Stacks are used for the return addresses of subroutine calls, for undo functions, and for evaluating expressions.
Queue — first in, first out
Operations: enqueue at the rear, dequeue at the front. Two pointers are needed. A linear queue wastes space as the front pointer moves up the array, so a circular queue wraps the pointers round using modulo arithmetic. Queues are used for print spooling, keyboard buffers and process scheduling.
Linked list
Each node holds data and a pointer to the next node; a start pointer gives the head and the last node points to null. Inserting or deleting means changing pointers, not shifting elements — which is why a linked list beats an array for frequent insertion in the middle. The cost is that you cannot jump straight to element $n$; you must follow the chain from the start.
Array or linked list?
Array: fast direct access by index, fixed size, expensive insertion. Linked list: cheap insertion and deletion, dynamic size, slow access, and extra memory for the pointers. State the trade-off, do not just say one is 'better'.
Worked example: tracing a stack and a queue on the same sequence of operations
Carry out the following sequence of operations, first on a STACK and then on a QUEUE:
add A, add B, add C, remove, add D, remove, add E, remove
State the order in which the elements are removed and the contents that remain.
Solution
Stack (last in, first out).
push A ⇒ [A]
push B ⇒ [A, B]
push C ⇒ [A, B, C]
pop ⇒ removes C, leaving [A, B]
push D ⇒ [A, B, D]
pop ⇒ removes D, leaving [A, B]
push E ⇒ [A, B, E]
pop ⇒ removes E, leaving [A, B]
Order removed: C, D, E. Remaining: A, B.
Queue (first in, first out).
enqueue A ⇒ [A]
enqueue B ⇒ [A, B]
enqueue C ⇒ [A, B, C]
dequeue ⇒ removes A, leaving [B, C]
enqueue D ⇒ [B, C, D]
dequeue ⇒ removes B, leaving [C, D]
enqueue E ⇒ [C, D, E]
dequeue ⇒ removes C, leaving [D, E]
Order removed: A, B, C. Remaining: D, E.
Worth taking away. The same sequence of operations gives opposite results: the stack returns exactly the three newest elements, the queue returns exactly the three oldest. That is why you must choose the right structure for the problem: an undo function needs a stack (undo the operation just made), while a print queue needs a queue (print in the order the jobs were sent).
Exam presentation tip: draw the contents of the structure after EACH operation instead of working it out in your head; mark schemes often give marks for intermediate steps.
Common mistake
Forgetting to check for overflow and empty when writing pseudocode. Apop function that does not check for an empty stack will return garbage or crash the program, and the mark scheme often has a separate mark for the statement IF top = 0 THEN OUTPUT "stack empty".