AS Level · Topic 10.4
A-Level Computer Science 9618 / Topic 10 / AS

Data with rules.

An abstract data type is defined by what you can do with it, not by how it is stored. Stacks, queues and linked lists are three ADTs you must be able to use, justify and build from arrays and pointers. You add, edit and delete data by hand and explain each step.

The key idea

An abstract data type (ADT) is a collection of data together with a set of operations on that data. A stack is last in, first out (LIFO): items are pushed onto and popped from the top, tracked by a top pointer. A queue is first in, first out (FIFO): items join at the rear and leave from the front; a circular queue reuses freed spaces. A linked list is a set of nodes, each holding data and a pointer to the next node, with a start pointer, a null pointer at the end and a free list of unused nodes. All three can be implemented using arrays.

Stack (LIFO) 794 TopOfStack push, pop here Queue (FIFO) CDE 01234 FrontRear remove at the front, add at the rear Linked list FigKiwiMango ∅ Start each node holds data and a pointer to the next node; the last pointer is null
Fig. 1: a stack, a queue and a linked list (the stack after PUSH 4, PUSH 9, PUSH 2, POP, PUSH 7)
Section 01

Stacks: last in, first out.

In the lab, push a few values, then pop them. Watch TopOfStack: it is the only pointer a stack needs. In the lab and in this lesson arrays start at index 0, so an empty stack has TopOfStack = −1. (With a lower bound of 1, an empty stack has TopOfStack = 0.)

Worked example Trace a stack
Stack is ARRAY[0:5] and TopOfStack starts at −1. Trace PUSH 4, PUSH 9, PUSH 2, POP, PUSH 7, POP, POP.
  1. PUSH 4, 9, 2: TopOfStack goes 0, 1, 2 and the stack holds 4, 9, 2 (bottom to top).
  2. POP returns 2; TopOfStack = 1.
  3. PUSH 7: TopOfStack = 2 and 7 overwrites the old 2 in Stack[2] (this is the state in Fig. 1).
  4. POP returns 7 (TopOfStack 1); POP returns 9 (TopOfStack 0).
Answer:
OperationTopOfStackStack (bottom to top)Returned
start−1empty
PUSH 404
PUSH 914, 9
PUSH 224, 9, 2
POP14, 92
PUSH 724, 9, 7
POP14, 97
POP049
Examiner trap

Do not say a pop "deletes" the value from the array. Only the pointer changes; the old value is simply overwritten by the next push.

Section 02

Queues: first in, first out.

In the lab, fill the queue, take two items off the front and try to add more. In a linear queue the spaces at the front are wasted; switch to the circular queue and the rear pointer wraps round to reuse them.

Worked example Trace a circular queue
Queue is ARRAY[0:4] with Front = 0, Rear = −1, Count = 0. Trace ENQUEUE A, B, C, D, then DEQUEUE twice, then ENQUEUE E, F.
  1. Enqueue A, B, C, D: Rear goes 0, 1, 2, 3 and Count becomes 4.
  2. Dequeue returns A (Front 1), then B (Front 2); Count is 2.
  3. Enqueue E: Rear = 4, Count = 3.
  4. Enqueue F: Rear = (4 + 1) MOD 5 = 0, so F goes into Queue[0], the space A left. Count = 4.
Answer: Front = 2, Rear = 0, Count = 4; the queue in order is C, D, E, F (indices 2, 3, 4, 0)
Your turn
Continue: ENQUEUE G, then ENQUEUE H. What happens?
Examiner trap

In a circular queue, Rear = upper bound does not mean the queue is full: Rear can wrap round. Test fullness with the count (or by comparing the pointers carefully).

Stage 1 · Learn

Check what the sim just showed you

Four quick checks. Each correct answer earns XP and records this skill on your profile.

Quick check+10 XP

Which ADT works on a last in, first out basis?

Quick check+10 XP

In the lab, POP is pressed when the stack is empty. What is reported?

Quick check+10 XP

A circular queue uses ARRAY[0:5]. RearPointer is 5 and the queue is not full. Where is the next item stored?

Quick check+10 XP

What does the pointer of the last node in a linked list hold?

Section 03

Linked lists.

In the lab, insert and delete items and watch only the pointers change: no data is moved. The same list can be written as two arrays (or one array of records with Data and Pointer fields), with −1 as the null pointer.

IndexDataPointer
0Kiwi3
1Apple4
25
3Mango−1
4Fig0
5−1

StartPointer = 1, FreeListPointer = 2. Following the pointers from node 1: Apple → Fig (4) → Kiwi (0) → Mango (3) → null, so the list is in alphabetical order even though the array is not. The free list is node 2 → node 5 → null.

Worked example Insert into an ordered list
Using the table above, insert Lime so that the list stays in alphabetical order. Give the new pointer values.
  1. Take node 2 from the free list; FreeListPointer becomes Pointer[2] = 5. Store Data[2] = Lime.
  2. Traverse from the start: Apple, Fig, Kiwi, then Mango, which comes after Lime. So Lime goes after Kiwi (node 0).
  3. New node first: Pointer[2] = Pointer[0] = 3 (Lime points to Mango).
  4. Then Pointer[0] = 2 (Kiwi points to Lime).
Answer: Pointer[0] = 2, Pointer[2] = 3; StartPointer = 1, FreeListPointer = 5. The list is Apple, Fig, Kiwi, Lime, Mango
Your turn
Start again from the original table and delete Fig. Which pointers change?
Examiner trap

When inserting, set the new node's pointer before changing the previous node's pointer. Doing it the other way round loses the address of the rest of the list.

Stage 2 · Exam

Exam-style questions

Unlocks once the four checks above are done. Worth more XP, written to AS Paper 1 and Paper 2 standard.

Finish the four checks above to unlock the exam questions
Exam style+20 XP

Which describes an abstract data type?

Exam style+20 XP

Which situation is best modelled by a queue?

Exam style+20 XP

An empty stack has the operations PUSH 3, PUSH 8, POP, PUSH 5, PUSH 1, POP. What does it hold, bottom to top?

Exam style+20 XP

Why might a linked list be used instead of an array for a list kept in order?

Skill unlocked

Stacks, queues and linked lists, mastered.

This skill is now recorded on your profile. Keep the chain going.

-Rank -Level -Score -Topics
Go deeper · practice
Six original Cambridge-style questions
Abstract data types, using stacks, queues and linked lists, tracing their pointers and implementing them with arrays.