Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python program to insert a new node at the beginning of the Doubly Linked list
When it is required to insert a new node at the beginning of a doubly linked list, a 'Node' class needs to be created. In this class, there are three attributes: the data that is present in the node, the access to the next node of the linked list, and the access to the previous node of the linked list.
Below is a demonstration for the same −
Node Class Structure
First, we define the Node class with three attributes ?
class Node:
def __init__(self, my_data):
self.prev = None
self.data = my_data
self.next = None
Doubly Linked List Implementation
Next, we create the doubly linked list class with methods to add nodes at the beginning and display the list ?
class Node:
def __init__(self, my_data):
self.prev = None
self.data = my_data
self.next = None
class DoublyLinkedList:
def __init__(self):
self.head = None
self.tail = None
def add_data_at_start(self, my_data):
new_node = Node(my_data)
if(self.head == None):
self.head = self.tail = new_node
self.head.prev = None
self.tail.next = None
else:
new_node.next = self.head
self.head.prev = new_node
new_node.prev = None
self.head = new_node
def print_it(self):
curr = self.head
if (self.head == None):
print("The list is empty")
return
print("The nodes in the doubly linked list are :")
while curr != None:
print(curr.data)
curr = curr.next
# Creating instance and adding elements
my_instance = DoublyLinkedList()
print("Elements are being added to the beginning of doubly linked list")
my_instance.add_data_at_start(10)
my_instance.print_it()
my_instance.add_data_at_start(24)
my_instance.print_it()
my_instance.add_data_at_start(54)
my_instance.print_it()
my_instance.add_data_at_start(77)
my_instance.print_it()
my_instance.add_data_at_start(92)
my_instance.print_it()
Output
Elements are being added to the beginning of doubly linked list The nodes in the doubly linked list are : 10 The nodes in the doubly linked list are : 24 10 The nodes in the doubly linked list are : 54 24 10 The nodes in the doubly linked list are : 77 54 24 10 The nodes in the doubly linked list are : 92 77 54 24 10
How It Works
The insertion process follows these steps:
- Empty List: If the list is empty, the new node becomes both head and tail
- Non-empty List: The new node's next pointer points to the current head, the current head's prev pointer points to the new node, and the new node becomes the new head
- Previous Pointer: The new head's prev pointer is always set to None
Key Components
- The Node class contains data, next, and prev attributes
- The DoublyLinkedList class manages head and tail pointers
- The add_data_at_start method handles insertion at the beginning
- The print_it method displays all nodes from head to tail
Conclusion
Inserting at the beginning of a doubly linked list requires updating both next and prev pointers. The new node becomes the head, and proper linking ensures the bidirectional structure is maintained.
