Lesson 76: Linked list
Pointers give the order
- A linked list keeps items in any free place in an array. Each node stores its data and a pointer: the index of the next node.
headPointergives the first node. A null pointer (here 0) means "this is the last node".- To add or remove an item, you change pointers. No item has to move.
| Index | data | pointer |
|---|---|---|
| 1 | Bridge | 3 |
| 2 | Station | 1 |
| 3 | Market | 4 |
| 4 | Park | 0 |
- headPointer = 2, so the route is Station → Bridge → Market → Park.

In the exam
- 9618/23/O/N/24 Q3: a linked list in an array List[1:200] of nodes, with HeadPointer and 0 as the null pointer. (a)(i) Why 0? Mark scheme: 0 is not a valid index of the array. (c) Describe how to output all the data in list order. Mark scheme: start with HeadPointer as the current pointer; repeat until the current pointer is 0; output the data of the current node; set the current pointer to that node's pointer field. That is your
printRoute.
Difficulty 4/5 · 280 coins + a small bonus on first completion today
A bus route is stored as a linked list in two arrays. data[i] is a stop and pointer[i] is the index of the next stop; 0 means "no next stop". headPointer is the first stop. The starter prints the stops in array order. Change printRoute so it follows the pointers: Route: Station Bridge Market Park.
Click Run to see the output here.
Difficulty 4/5 · 280 coins + a small bonus on first completion today
Type a new stop in the Serial Monitor. Finish addAtEnd: put the name in the free node data[nextFree] with pointer 0, follow the pointers to the last node, and make it point to the new node. Then add 1 to nextFree.
Click Run to see the output here.