0
0
Data Structures Theoryknowledge~6 mins

Doubly linked list in Data Structures Theory - Full Explanation

Choose your learning style9 modes available
Introduction
Imagine you want to move forwards and backwards easily through a list of items, like flipping pages in a photo album. A doubly linked list solves this by letting you go in both directions smoothly.
Explanation
Structure of Nodes
Each item in a doubly linked list is called a node. Every node holds the actual data and has two links: one pointing to the next node and one pointing to the previous node. This setup allows easy movement forward and backward through the list.
Nodes have two links, enabling navigation in both directions.
Head and Tail
The list has a special node called the head, which is the first item, and another called the tail, which is the last item. The head's previous link and the tail's next link are usually empty or null, marking the ends of the list.
Head and tail mark the start and end, with null links indicating list boundaries.
Traversal
You can move through the list starting from the head going forward by following next links, or from the tail going backward by following previous links. This makes it easy to access items in either direction without starting over.
Traversal can happen forwards or backwards thanks to two-way links.
Insertion and Deletion
Adding or removing nodes is flexible because you can adjust the links of neighboring nodes in both directions. For example, to insert a node, you update the previous node's next link and the next node's previous link to include the new node.
Insertion and deletion involve updating links in both directions for smooth list changes.
Real World Analogy

Think of a train where each carriage is connected to the one before and after it. You can walk from the front carriage to the back or go back the other way easily. If you add or remove a carriage, you just connect the neighboring carriages again.

Structure of Nodes → Each carriage holding passengers and connected to the next and previous carriages
Head and Tail → The first and last carriages of the train marking the ends
Traversal → Walking forwards or backwards through the train carriages
Insertion and Deletion → Adding or removing a carriage by linking the neighboring carriages together
Diagram
Diagram
┌───────┐    ┌───────┐    ┌───────┐
│ Prev  │←──▶│ Prev  │←──▶│ Prev  │
│ Data  │    │ Data  │    │ Data  │
│ Next  │──▶ │ Next  │──▶ │ Next  │
└───────┘    └───────┘    └───────┘
  Head                        Tail
This diagram shows three nodes linked forward and backward, with head at the start and tail at the end.
Key Facts
NodeA container holding data and two links: one to the next node and one to the previous node.
HeadThe first node in the doubly linked list with no previous node.
TailThe last node in the doubly linked list with no next node.
TraversalMoving through the list forward or backward by following next or previous links.
InsertionAdding a new node by updating the links of neighboring nodes in both directions.
DeletionRemoving a node by reconnecting its previous and next nodes to each other.
Code Example
Data Structures Theory
class Node:
    def __init__(self, data):
        self.data = data
        self.prev = None
        self.next = None

class DoublyLinkedList:
    def __init__(self):
        self.head = None

    def append(self, data):
        new_node = Node(data)
        if not self.head:
            self.head = new_node
            return
        last = self.head
        while last.next:
            last = last.next
        last.next = new_node
        new_node.prev = last

    def traverse_forward(self):
        current = self.head
        while current:
            print(current.data, end=' ')
            current = current.next
        print()

    def traverse_backward(self):
        current = self.head
        if not current:
            return
        while current.next:
            current = current.next
        while current:
            print(current.data, end=' ')
            current = current.prev
        print()

# Example usage
my_list = DoublyLinkedList()
my_list.append(10)
my_list.append(20)
my_list.append(30)
my_list.traverse_forward()
my_list.traverse_backward()
OutputSuccess
Common Confusions
Thinking a doubly linked list only moves forward like a simple list.
Thinking a doubly linked list only moves forward like a simple list. A doubly linked list allows movement both forward and backward because each node links to both neighbors.
Believing the head node has a previous node or the tail node has a next node.
Believing the head node has a previous node or the tail node has a next node. The head's previous link and the tail's next link are always null, marking the ends of the list.
Summary
A doubly linked list lets you move forward and backward through items easily.
Each node connects to the next and previous nodes, with special head and tail nodes marking the ends.
You can add or remove items by updating links in both directions without starting over.