Abstract Data Types
Abstract Data Type (ADT): a collection of data and a set of operations on that data. Three ADTs: Stack · Queue · Linked List.
1. Stack
Definition: a list of items operating on the last-in-first-out (LIFO) principle. Items are added with PUSH and removed with POP. The first item added is the last removed.
- TopOfStack pointer → the last item added
- BottomOfStack pointer → the first item on the stack
- the stack grows upwards when items are added
- POP on an empty stack → error (underflow); PUSH on a full stack → can't add (overflow)
Push("Taha"), Push("Ali"), Push("Amjad"), Push("Bano"), Pop(), Pop(), Push("Qasim")
| Operation | Stack (bottom → top) |
|---|---|
| Push Taha | Taha |
| Push Ali | Taha, Ali |
| Push Amjad | Taha, Ali, Amjad |
| Push Bano | Taha, Ali, Amjad, Bano |
| Pop → Bano removed | Taha, Ali, Amjad |
| Pop → Amjad removed | Taha, Ali |
| Push Qasim | Taha, Ali, Qasim (top = Qasim) |
Each character is pushed one by one, then all are popped. The stack is Last In First Out, so the first character added ("P") is removed last — the order is reversed.
Stack (bottom → top): red, blue, green, yellow.
(ii) step by step: POP removes orange, POP removes purple, brown is pushed then popped straight away, black is pushed.
Memory locations 500 → 505: PPP (500, BottomOfStack), NNN, ZZZ, XXX, YYY, WWW (505, TopOfStack). 506 empty.
| Step | Effect |
|---|---|
| POP → Data1 | Data1 = WWW, top = 504 (YYY) |
| POP → Data2 | Data2 = YYY, top = 503 (XXX) |
| PUSH AAA | 504 = AAA, top = 504 |
| PUSH BBB | 505 = BBB, top = 505 |
| POP (discard) | BBB discarded, top = 504 |
| POP → Data2 | Data2 = AAA, top = 503 |
Final: 500 PPP · 501 NNN · 502 ZZZ · 503 XXX ← TopOfStack · (504 AAA, 505 BBB left in memory but above the pointer) · BottomOfStack at 500 · Data1 = WWW · Data2 = AAA. Marks: BBB & AAA values · XXX→PPP unchanged · both pointers labelled · both variables.
Alternative without POP: read the value at TopOfStack · write it · decrement ToS · repeat.
Stack (top → bottom): D1, D3, D4, D5, D2. The array is filled from the lowest index upwards and all 8 elements are available.
| Array element | 1 | 2 | 3 | 4 | 5 | 6–8 |
|---|---|---|---|---|---|---|
| Data | D2 | D5 | D4 | D3 | D1 | empty |
TopOfStack = 5 (index of D1) · BottomOfStack = 1 (index of D2). The first item pushed (D2) is at the bottom/lowest index.
(b) Push() — complete the five steps:
2. Queue
Definition: a list operating on the first-in-first-out (FIFO) principle — the first item added is the first removed. Data is added at the rear using the EndPointer and removed from the front using the StartPointer. (Enqueue = add, Dequeue = remove)
- Front of queue pointer → the next item to be removed
- End of queue pointer → the last item added
Enqueue("Taha"), Enqueue("Ali"), Enqueue("Amjad"), Enqueue("Bano"), Dequeue(), Dequeue(), Enqueue("Qasim")
| Operation | Queue (front → rear) |
|---|---|
| 4 × Enqueue | Taha, Ali, Amjad, Bano |
| Dequeue → Taha out | Ali, Amjad, Bano |
| Dequeue → Ali out | Amjad, Bano |
| Enqueue Qasim | Amjad, Bano, Qasim |
Linear vs circular queue
Front pointer → location 5 (Red); items Red 5, Green 6, Blue 7, Pink 8 (End pointer); 9 empty. Max 10 items.
Jobs A, B, C, D arrived in that order; Start pointer at 2nd space, End pointer at 5th space.
The board's attempted answer wrote the jobs as A B D C — arrival order must be kept (C before D).
Frog 0 (front), Cat 1, Fish 2, Elk 3 (end); 4–7 empty.
FrontOfQueue, EndOfQueue and NumItems (number of items); queue (front → end) = D3, D4, D1, D2, D5. Pointer variables store array indices.
| Index | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|
| Array | D3 | D4 | D1 | D2 | D5 |
FrontOfQueue = 2 · EndOfQueue = 6 · NumItems = 5 (mark scheme accepts the items in any five consecutive locations, in order or reversed, with pointers matching D3 and D5).
(b) AddTo() — circular queue, check it is not full. Complete the six steps:
Step 5 marking: 1 mark for "element", 1 mark for the other two terms.
3. Linked List
Definition: a data structure that stores a collection of items where each item (node) is linked to the next one using pointers.
- Start pointer → first node of the list · Free-list pointer → first unused node (the free list is another linked list of empty nodes)
- Null pointer = end of list: accept 0 / Ø / −1 / an out-of-bounds index
- implemented with two 1D arrays (Data array + Pointer array) and two variables — pointers store index numbers
Ptr1 = start pointer: D40 → D32 → D11 → D100 → Ø. Ptr2 = free-list pointer: F1 → F2 → F3 → F4 → Ø. Some nodes already filled in: index 1 D32 (pointer 2) · index 2 pointer 3 · index 4 D40 · index 6 F2 (pointer 7) · Free_List_Pointer = 5.
| Variable | Value |
|---|---|
| Start_Pointer | 4 |
| Free_List_Pointer | 5 |
| Index | Data array | Pointer array | Which list? |
|---|---|---|---|
| 1 | D32 | 2 | list → next is index 2 (D11) |
| 2 | D11 | 3 | list → index 3 (D100) |
| 3 | D100 | 0 | list, last node (null = 0) |
| 4 | D40 | 1 | list, first node (Start_Pointer = 4) |
| 5 | F1 | 6 | free list, first (Free_List_Pointer = 5) |
| 6 | F2 | 7 | free list |
| 7 | F3 | 8 | free list |
| 8 | F4 | 0 | free list, last (null) |
Marks: 1 for Start_Pointer + 1 for each group of rows: 2 · 3 and 4 · 5 · 7 and 8. How to read: start at Start_Pointer 4 → D40 → pointer 1 → D32 → pointer 2 → D11 → pointer 3 → D100 → pointer 0 = end.
Insert a node (generally)
- Check for a free node in the linked list (free list not empty)
- Search for the correct position (ordered list)
- Assign the value (e.g. B) to the first node in the free list
- The pointer from B is changed to point to C
- The pointer from A points to B
- The free-list start pointer moves to point to the next free node
Delete a node
- Search for the node you want to delete — start from the first node and follow the pointers
- If it is the first node: point the start pointer to the next node in the list
- If it is in the middle: change the pointer of the previous node to point to the node after the deleted one
- Point the free-list pointer towards the removed node (it becomes the new first free node, pointing to the old first free node)
startPointer = 0 · emptyList = 5 (the free list). Records:
| Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
|---|---|---|---|---|---|---|---|---|---|---|
| data | 1 | 5 | 6 | 7 | 2 | 0 | 0 | 56 | 0 | 0 |
| nextNode | 1 | 4 | 7 | −1 | 2 | 6 | 8 | 3 | 9 | −1 |
Reading it: list from startPointer 0 → 1 (idx 0) → 5 (idx 1) → 2 (idx 4) → 6 (idx 2) → 56 (idx 7) → 7 (idx 3) → −1 (end). Free list from emptyList 5 → idx 5 → 6 → 8 → 9 → −1. Here −1 is the null pointer and the list is unordered (1, 5, 2, 6, 56, 7). (The slide shows the table; the sub-questions asked about it were not on the page I could see.)
- ADT = data + operations · Stack, Queue, Linked list
- Stack: LIFO · PUSH / POP · TopOfStack, BottomOfStack · full if ToS = max · empty if no items · "reverse" = LIFO
- Queue: FIFO · add at end (EoQ ↑), remove at front (FoQ ↑) · circular: wrap round (after last index → first) · full when NumItems = size
- Same front & end pointer ⇒ exactly one item in the queue
- Linked list: node = data + pointer · Start pointer + Free-list pointer · null = 0 / Ø / −1
- Insert: take first free node → set its pointer → relink previous node → move free pointer · Delete: relink previous node (or start pointer) → add node to free list