Doubly Linked List Introduction

0

Introduction to Doubly Linked Lists

In this lesson, we’re going to begin working with Doubly Linked Lists.
Our goal is to create the constructor that initializes this data structure.

Before we dive into code, let’s start by reviewing what we already know from Singly Linked Lists.


🧱 Revisiting Singly Linked Lists

In a singly linked list, each node stores:

  • A value

  • A pointer to the next node (next)

Visually, it looks like this:

[ Value | Next ][ Value | Next ]None

And the node constructor looked like this:

class Node:
       def __init__(self, value):
                  self.value = value
                  self.next = None

🔁 Transitioning to a Doubly Linked List

A doubly linked list is very similar, but each node has two pointers:

  • One pointing to the next node

  • Another pointing to the previous node

This allows traversal in both directions — forward and backward.

Visually, it looks like this:

None[Prev | Value | Next][Prev | Value | Next]None

🧩 Step 1: Creating the Node for a Doubly Linked List

Just like before, we’ll create a Node class — but this time, we’ll add one extra property: previous.

Here’s the code:

class Node:
       def __init__(self, value):
                   self.value = value
                  self.next = None
                  self.previous = None

✅ The only difference between this and the singly linked list node is the additional pointer:

self.previous = None

Initially, both next and previous are None, since the node isn’t connected yet.


🧩 Step 2: Building the Doubly Linked List Class

Now that we can create nodes, let’s build our main DoublyLinkedList class.

This class will:

  1. Accept a value to create the first node.

  2. Set that node as both the head and the tail.

  3. Initialize the length of the list to 1.

Here’s how the constructor looks:

class DoublyLinkedList:
         def __init__(self, value):
                    new_node = Node(value)
                    self.head = new_node
                    self.tail = new_node
                   self.length = 1

🧠 How This Works

When you run:

my_dll = DoublyLinkedList(7)

It will:

  • Create a new node with value 7.

  • Set both head and tail to point to this node.

  • Set the length to 1.

So visually, your structure looks like this:

Head[ None7None ]Tail
Length = 1

🖨️ Step 3: Adding a Print Method (for Visualization)

Just like with singly linked lists, we’ll create a helper method to print out the values.

def print_list(self):
        temp = self.head
        while temp:
               print(temp.value)
             temp = temp.next

This helps us see the current state of our list.


💻 Step 4: Running the Code in VS Code

Here’s the full setup:

class Node:
      def __init__(self, value):
                 self.value = value
                 self.next = None
                self.previous = None

class DoublyLinkedList:

       def __init__(self, value):
              new_node = Node(value)
            self.head = new_node
           self.tail = new_node
           self.length = 1
     def print_list(self):
         temp = self.head
                  while temp:
                         print(temp.value)

                        temp = temp.next

# Creating a new doubly linked list

my_dll = DoublyLinkedList(7)
my_dll.print_list()

Output:

7

✅ Result

Everything works perfectly!
We successfully created:

  • A Node class with value, next, and previous.

  • A DoublyLinkedList constructor that initializes head, tail, and length.


🧠 Key Takeaways

  • A doubly linked list allows two-way traversal.

  • Each node has value, next, and previous pointers.

  • The constructor initializes:

    • Head → first node

    • Tail → first node

    • Length → 1

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top