Let’s Explore Linked Lists: Understanding Linked Lists through Trains

Ifte Refat
5 min readJan 9, 2024

--

In this blog post, we will explore the concept of linked lists uniquely, their types, advantages, and common use cases. I’ll try to teach you a linked list using the train concept.

Concepts of Linked List

Linked lists are like digital trains carrying important information on each car! They’re super useful in organizing data on computers, connecting one piece to the next seamlessly. Efficient and flexible, linked lists are the unsung heroes of the digital world!

Understanding Linked Lists through Trains 🚂:

Imagine a train system where each train car is like a node in a linked list. Let’s break down the comparison:

Components of a Linked List:

1. Train Car (Node):

  • In a train, each car holds cargo and connects to the next car.
  • Each node holds data and links to the next node.

# Node class for a Linked List
class Node:
"""
Represents a node in a linked list.

Attributes:
data: Information stored in the node.
next: Reference to the next node in the linked list.
"""

# Initialize the node object
def __init__(self, data):
"""
Constructor to initialize a new node with given data.

Parameters:
data: Information to be stored in the node.
"""
self.data = data # Assign data to the node
self.next = None # Initialize next as null (to be updated when adding a new node)

2. Engine (Head):

  • The front of the train is the engine, leading the way.
  • In a linked list, the head is like the engine, pointing to the first node.
# LinkedList class
class LinkedList:
"""
Represents a linked list.

Attributes:
head: Reference to the first node in the linked list.
"""

# Initialize the linked list
def __init__(self):
"""
Constructor to initialize an empty linked list.
"""
self.head = None # Initially, the list is empty, so the head points to None

3. Last Car (Tail):

  • The last car signals the end of the train.
  • In a linked list, the tail is the final node, pointing to nothing (null) to mark the end.

Different Types of Linked Lists:

1. Train Cars in a Row (Singly Linked List):

  • Train cars are connected one after another.
  • In a singly linked list, nodes are linked in a single chain.
# Node class for a Singly Linked List
class Node:
"""
Represents a node in a singly linked list.

Attributes:
data: Information stored in the node.
next: Reference to the next node in the linked list.
"""

# Initialize the node object
def __init__(self, data):
"""
Constructor to initialize a new node with given data.

Parameters:
data: Information to be stored in the node.
"""
self.data = data # Assign data to the node
self.next = None # Initialize next as null (to be updated when adding a new node)


# SinglyLinkedList class
class SinglyLinkedList:
"""
Represents a singly linked list.

Attributes:
head: Reference to the first node in the linked list.
"""

# Initialize the linked list
def __init__(self):
"""
Constructor to initialize an empty singly linked list.
"""
self.head = None # Initially, the list is empty, so the head points to None

2. Train Cars with Doors on Both Sides (Doubly Linked List):

  • Some trains have doors on both sides of the cars.
  • In a doubly linked list, nodes have links to both the next and previous nodes.
# Node class for a Doubly Linked List
class Node:
"""
Represents a node in a doubly linked list.

Attributes:
data: Information stored in the node.
next: Reference to the next node in the linked list.
prev: Reference to the previous node in the linked list.
"""

# Initialize the node object
def __init__(self, data):
"""
Constructor to initialize a new node with given data.

Parameters:
data: Information to be stored in the node.
"""
self.data = data # Assign data to the node
self.next = None # Initialize next as null (to be updated when adding a new node)
self.prev = None # Initialize prev as null (to be updated when adding a new node)


# DoublyLinkedList class
class DoublyLinkedList:
"""
Represents a doubly linked list.

Attributes:
head: Reference to the first node in the linked list.
tail: Reference to the last node in the linked list.
"""

# Initialize the linked list
def __init__(self):
"""
Constructor to initialize an empty doubly linked list.
"""
self.head = None # Initially, the list is empty, so the head points to None
self.tail = None # Initially, the list is empty, so the tail points to None

3. Train in a Loop (Circular Linked List):

  • Imagine a train moving in a loop with the last car connected to the first.
  • A circular linked list forms a loop, with the tail pointing back to the head.
# Node class for a Circular Linked List
class Node:
"""
Represents a node in a circular linked list.

Attributes:
data: Information stored in the node.
next: Reference to the next node in the linked list.
"""

# Initialize the node object
def __init__(self, data):
"""
Constructor to initialize a new node with given data.

Parameters:
data: Information to be stored in the node.
"""
self.data = data # Assign data to the node
self.next = None # Initialize next as null (to be updated when adding a new node)


# CircularLinkedList class
class CircularLinkedList:
"""
Represents a circular linked list.

Attributes:
head: Reference to the first node in the linked list.
"""

# Initialize the linked list
def __init__(self):
"""
Constructor to initialize an empty circular linked list.
"""
self.head = None # Initially, the list is empty, so the head points to None

Train Operations (Linked List Tasks):

Adding a New Car (Node):

Attach a new car to the front, middle, or end of the train.

2.Removing a Car (Node):

Detach a car from the train, leaving the rest connected.

3.Checking Each Car (Node):

Go through each car in the train, just like traversing nodes in a linked list.

4.Finding a Specific Cargo (Node with Specific Data):

Look for a particular cargo in a train, similar to finding a node with specific data in a linked list.

Advantages of Linked Lists:

Dynamic Size:

  • Can easily grow or shrink during runtime.

Efficient Insertion and Deletion:

  • Inserting or deleting nodes is more efficient compared to arrays.

No Pre-allocation of Memory:

  • Memory is allocated as needed, avoiding wasted space.

Where Do We See Linked Lists?

Remembering Stuff in Apps:

  • Apps use linked lists to remember things that might change a lot.

Stacking and Queuing:

  • Linked lists help in making stacks (like plates in a restaurant) and queues (like people waiting in line).

Undo Buttons in Apps:

  • Ever made a mistake in a drawing app and pressed undo? Linked lists help make that magic happen!

Conclusion:

Understanding linked lists through the analogy of trains helps simplify the concept. Just as trains efficiently transport cargo, linked lists are dynamic structures that efficiently manage and organize data in a flexible manner.

Thank You, Guys, For reading all the way through. Hope You guys understand the basic concept of LinkedList.

--

--

Ifte Refat

An engineering student . I'll write about my daily learning progress in my profile .