Menu
Theme
Bachelor of Science in Computer Science
Course Content

Linked Lists

Data Structures & Algorithms

Unchain Your Data: A Deep Dive into Linked Lists!

Habari mwanafunzi! Ever been in a long queue, say for a matatu at Kencom during rush hour? You know who is in front of you, and someone else knows you are in front of them. You are all linked together in a line. Even if someone leaves the queue or someone else joins, the line just adjusts. That, my friend, is the basic idea behind a Linked List!

Forget about rigid, fixed-size boxes like Arrays for a moment. Today, we venture into the world of dynamic, flexible data structures that can grow and shrink as we need them to. Let's get started!

What Exactly is a Linked List?

A linked list is a linear data structure, but unlike an array, its elements are not stored at a contiguous location. Instead, it is a chain of 'nodes' linked together.

Think of it like a treasure hunt. Each clue (a node) has two parts:

  • The information for that step of the hunt (the data).
  • Directions to the next clue (a pointer or a link to the next node).

You start at the beginning (the Head), follow the directions, and you can get through the entire chain until you reach the last clue, which points to nowhere (NULL).

Kenyan Analogy: The Boda Boda Relay

Imagine you need to send a package from Nairobi CBD to Ngong. Instead of one boda boda rider going the whole way, you use a relay.
- Rider 1 (Head) in CBD has the first part of the package and knows the location of Rider 2 in Westlands.
- Rider 2 in Westlands takes the package, adds his part, and knows the location of Rider 3 in Karen.
- Rider 3 in Karen takes it and knows the location of the final drop-off in Ngong (Tail).
Each rider is a node. The package part is the data, and the knowledge of the next rider's location is the link.

The Anatomy of a Node

The fundamental building block of a linked list is the Node. It's a simple container with two sections:

  1. Data: The actual value we want to store (e.g., a student's ID number, a name, a price).
  2. Next Pointer: A reference (an "arrow" or memory address) that points to the very next node in the chain. The last node's 'next' pointer points to NULL, indicating the end of the list.

// A visual representation of a single Node

+-------------+------+
|    Data     | Next |
+-------------+------+
| "KPLC Token"|  *----->
+-------------+------+
Image Suggestion:

An animated, friendly 3D illustration. Show a series of glowing, semi-transparent cubes floating in a line. The first cube is labeled 'HEAD'. Each cube contains a simple icon (like a music note or a user profile) representing 'Data'. A glowing, animated arrow emerges from one cube and points directly to the next cube in the line, representing the 'Next' pointer. The last cube's arrow points to a fading word 'NULL'. The style should be futuristic and clean.

Putting it all Together: The Chain

When we link these nodes together, we form a Linked List. The very first node is called the HEAD. It's our entry point to the list. Without knowing where the HEAD is, we'd lose the entire list!


// A simple Linked List with three nodes

  (HEAD)
    |
    v
+-------+------+     +-------+------+     +-------+------+
|  Data: 10   | *--->|  Data: 20   | *--->|  Data: 30   | *---> NULL
+-------+------+     +-------+------+     +-------+------+
                                                 (TAIL)

Types of Linked Lists

Just like we have different types of roads (one-way, two-way), we have different types of linked lists.

  • Singly Linked List: The most basic type. Each node only has a pointer to the next node. It's like a one-way street; you can only move forward.
  • Doubly Linked List: This is more like Thika Highway! Each node has two pointers: one to the next node and another to the previous node. This allows you to traverse the list forwards and backwards. Super useful!

// A Node in a Doubly Linked List

+----------+-------+------+
| Previous |  Data | Next |
+----------+-------+------+
|   <----* |  20   | *---->
+----------+-------+------+
  • Circular Linked List: In this type, the 'next' pointer of the last node (the Tail) doesn't point to NULL. Instead, it points back to the first node (the Head)! It's like the Westlands roundabout – you can keep going in a loop forever.

Key Operations (The Fun Part!)

So, what can we do with these lists? Let's look at the main operations.

1. Traversal (Kutembea - to walk through)

This means visiting every node in the list, starting from the head. It's like a matatu conductor starting from the front seat and collecting fare from every passenger one by one until they reach the back.

2. Insertion (Kuongeza - to add)

This is where linked lists shine! Adding a new node is very efficient. We just need to adjust a couple of pointers.

  • At the beginning: The new node points to the old Head, and we declare our new node as the new Head. Easy!
  • At the end: We traverse to the last node, and make its 'next' pointer point to our new node.
  • In the middle: We find the position, then make the previous node point to our new node, and our new node point to the next one. We just 'stitch' it in.

3. Deletion (Kutoa - to remove)

Need to remove a node? No problem. We find the node before the one we want to delete and change its 'next' pointer to "skip" over the deleted node, pointing to the one after it. The skipped node is then removed.


// Deleting Node 'B' from A -> B -> C

// Before:
// A -> B -> C

// Step 1: Find node 'A' (the one before 'B').
// Step 2: Change A's 'next' pointer to point to C.

// After:
// A -> C
// (B is now unlinked and can be deleted)

Linked Lists vs. Arrays: The Showdown

When would you use a linked list instead of our good old array?

An array is like a public parking garage with numbered slots. The slots are all next to each other, and you can instantly go to slot #57. But, the garage has a fixed size. If it's full, it's full! And if you want to add a car in the middle, you have to move all the other cars!

A linked list is like valet parking. You don't know where your car is parked, but you don't need to. You just have a ticket (the Head). The valet knows where your car is, and that car has a "note" on it saying where the next car is. You can add as many cars as you want (dynamic size), and adding or removing one doesn't require shifting all the others.

Let's Get Practical: A Simple Python Example

Here’s how you might define a Node and link a few of them together in Python, a language you'll likely use.


# First, we define what a Node looks like
class Node:
    def __init__(self, data):
        self.data = data  # The data the node holds
        self.next = None  # The pointer to the next node

# Now, let's create some nodes!
# These are like our individual students in a line.
node1 = Node("Amina")
node2 = Node("Brian")
node3 = Node("Catherine")

# Now, let's link them up to form a list.
# This is like telling each student who is behind them.
print("Creating the links...")
node1.next = node2  # Amina points to Brian
node2.next = node3  # Brian points to Catherine

# Let's traverse the list starting from the head (node1)
current_node = node1
while current_node is not None:
    print(current_node.data)
    current_node = current_node.next # Move to the next node

# Expected Output:
# Creating the links...
# Amina
# Brian
# Catherine

Conclusion

Congratulations! You've just unchained your understanding of data structures. Linked lists are powerful because of their dynamic size and efficient insertion/deletion. They are the backbone of many things you use daily, from your music player's playlist to your browser's history ("back" button).

While they might seem a bit tricky at first, remember the matatu queue or the boda boda relay. Once you can visualize the nodes and the pointers, you've mastered the core concept.

Tafakari (Reflection Point):

Think about the "Undo" feature in Microsoft Word or Google Docs. How could a Doubly Linked List be used to implement this? What would the `data`, `next`, and `previous` pointers represent?

Keep practicing, keep coding, and you'll see these structures everywhere! You've got this.

Pro Tip

Take your own short notes while going through the topics.

Previous I/O
KenyaEdu
Add KenyaEdu to Home Screen
For offline access and faster experience