10.4.4 Linked Lists: Nodes, Pointers and Updates
A linked list stores a sequence of nodes. Each node contains data and a link to the next node. The nodes do not need to occupy neighbouring storage locations because the links, rather than physical position, determine the list order.
This page focuses on the behaviour of a singly linked list: following links, inserting nodes, editing stored values and removing nodes. Detailed parallel-array layouts, free-list management and comparisons with stack and queue implementations are reserved for 10.4.5 Implementing ADTs with Arrays.
By the end of this section, you should be able to:
- Describe a linked list as a sequence of connected nodes.
- Use the terms node, pointer, start pointer and null pointer accurately.
- Trace the logical order of a list by following pointers.
- Explain pointer changes for insertion at the start, middle and end.
- Explain pointer changes for deletion at the start, middle and end.
- Distinguish editing node data from changing the links between nodes.
- Justify the use of a linked list for a given situation.
- State the main benefits and limitations of linked lists.
The Linked List Model
A fixed array normally gives each item a position such as index 0, 1, 2 and 3. In a linked list, the next item is found by reading a pointer stored in the current node. This allows the logical sequence to differ from the physical storage order.
Key Terms
| Term | Meaning | Role in a list |
|---|---|---|
| Node | One linked-list element containing data and a pointer field. | Stores one item and identifies what comes next. |
| Pointer | A stored value that identifies another node's location. | Connects the current node to its successor. |
| Start pointer | A variable identifying the first node. | Provides the entry point for traversal. |
| Null pointer | A special value meaning βno next nodeβ. | Marks the end of the list; array examples often use -1. |
| Traversal | Visiting nodes by repeatedly following their pointers. | Used to find, display or update an item. |
Tracing the Logical Order
Consider nodes stored at non-consecutive indexes:
| Storage index | Data | Next pointer |
|---|---|---|
| 0 | Echo | 5 |
| 2 | Amber | 6 |
| 4 | Quartz | -1 |
| 5 | Kite | 4 |
| 6 | Cedar | 0 |
If StartPointer = 2, trace from index 2:
- Index 2 stores Amber and points to index 6.
- Index 6 stores Cedar and points to index 0.
- Index 0 stores Echo and points to index 5.
- Index 5 stores Kite and points to index 4.
- Index 4 stores Quartz and points to -1, so the traversal stops.
Inserting Nodes
Insert at the start
The new node must point to the old first node. The start pointer is then changed to identify the new node.
Insert between two nodes
Suppose Dune must be inserted between Cedar and Echo. Dune first receives Cedar's old pointer, then Cedar is changed to point to Dune.
Insert at the end
The old final node is changed to point to the new node. The new node receives a null pointer because it is now last.
Deleting Nodes
Deletion removes a node from the active chain. The old data may still remain physically stored until the location is reused.
| Position removed | Pointer update | Example |
|---|---|---|
| First node | Copy the first node's next pointer into the start pointer. | Start moves from Amber to Cedar. |
| Middle node | Make the predecessor point to the deleted node's successor. | Cedar changes from pointing to Echo to pointing to Kite. |
| Final node | Set the predecessor's pointer to null. | Kite becomes the final node after Quartz is removed. |
Editing Data Without Changing the Links
To edit a stored value, traverse from the start pointer until the required node is found, then change its data field. The pointer can remain unchanged when the node's position in the sequence is still correct.
Maintaining an Ordered Linked List
A linked list may be kept in key order, such as alphabetical order or ascending priority. The physical storage locations still do not need to be ordered.
To insert a new key, traverse the list until the correct predecessor and successor are found. Then change the two links needed to place the new node between them.
[Echo, Willow, Amber, Dune, Quartz, Kite, Cedar]while the linked order is: Amber β Cedar β Dune β Echo β Kite β Quartz β Willow.
When Is a Linked List Suitable?
| Requirement | How a linked list responds |
|---|---|
| Items are inserted or deleted frequently. | Links can be changed without shifting every later item. |
| The number of items changes over time. | Nodes can be added or released as needed, depending on implementation. |
| Items are normally processed sequentially. | Traversal naturally follows one node to the next. |
| Direct access to item number 500 is required. | A linked list is less suitable because earlier nodes must be traversed first. |
| Memory use must be minimal. | Pointer fields add storage overhead. |
Implementation Preview
A linked list can be represented using parallel arrays or an array of node records. One field stores data and another stores the index of the next node. A separate start pointer identifies the first active node.
| Implementation component | Purpose |
|---|---|
| Data field or data array | Stores the node's value or record. |
| Next field or pointer array | Stores the index of the successor. |
| Start pointer | Stores the index of the first active node. |
| Unused-node management | Tracks storage positions available for later insertion. |
Interactive: Linked List Update Lab
Select a scenario and step through the pointer changes. The left-hand table shows physical storage; the right-hand chain shows the logical list produced by following the start pointer.
Common Mistakes and Misconceptions
- Reading nodes in array-index order instead of following the start pointer.
- Confusing the node's data with its pointer.
- Treating null as an empty string or zero data value.
- Forgetting to preserve the old next pointer during a middle insertion.
- Setting the predecessor to null when deleting a middle node.
- Assuming deleted data must be erased immediately.
- Claiming linked lists provide fast direct access by position.
- Ignoring the extra storage required for pointer fields.
- Including detailed free-list mechanics when the question only asks about conceptual updates.
Practice
Task 1: Trace a list
StartPointer = 4. Node 4 points to 1, node 1 points to 6,
node 6 points to 2 and node 2 points to -1.
- Write the index traversal.
- State which node is first and which is last.
Task 2: Insert at the start
The current start pointer is 5. A new node is stored at index 3. State the two pointer updates required to make index 3 the new first node.
Task 3: Insert in the middle
Node P currently points to node R. Explain how node Q can be inserted between them without losing the link to R.
Task 4: Delete a node
The chain is M β N β P β S β NULL.
- Describe the pointer change needed to remove P.
- Describe the pointer change needed to remove M.
- Describe the pointer change needed to remove S.
Task 5: Choose and justify
Decide whether a linked list is suitable for each situation.
- A playlist where tracks are frequently inserted and removed.
- A fixed table requiring immediate access to row 900.
- A changing sequence of large records processed from start to end.
- A tiny fixed set of values that never changes order.
Review
| Question | Strong answer should include |
|---|---|
| What is a node? | An element containing data and a pointer to the next node. |
| What does the start pointer do? | Identifies the first node and provides the entry point for traversal. |
| What marks the end of the list? | A null pointer in the final node. |
| How is a middle node inserted? | The new node receives the predecessor's old pointer, then the predecessor points to the new node. |
| How is a middle node deleted? | The predecessor is changed to point to the target's successor. |
| What is a main benefit? | Insertion and deletion can avoid shifting many stored values. |
| What are two limitations? | Sequential traversal is needed and pointer fields consume extra storage. |