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 middle of the Doubly Linked List
When you need to insert a new node in the middle of a doubly linked list, you create a Node class with three attributes: the data, access to the next node, and access to the previous node. This allows bidirectional traversal and insertion at any position.
Node Class Structure
The Node class defines the structure of each element in the doubly linked list ?
class Node:
def __init__(self, my_data):
self.previous = None
self.data = my_data
self.next = None
Doubly Linked List Implementation
Here's the complete implementation with methods to add nodes and insert at the middle position ?
class Node:
def __init__(self, my_data):
self.previous = None
self.data = my_data
self.next = None
class DoublyLinkedList:
def __init__(self):
self.head = None
self.tail = None
self.size = 0
def add_data(self, my_data):
new_node = Node(my_data)
if self.head == None:
self.head = self.tail = new_node
self.head.previous = None
self.tail.next = None
else:
self.tail.next = new_node
new_node.previous = self.tail
self.tail = new_node
self.tail.next = None
self.size = self.size + 1
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
def add_data_in_middle(self, my_data):
new_node = Node(my_data)
if self.head == None:
self.head = self.tail = new_node
self.head.previous = None
self.tail.next = None
else:
curr = self.head
mid = (self.size//2) if(self.size % 2 == 0) else ((self.size+1)//2)
for i in range(1, mid):
curr = curr.next
temp = curr.next
curr.next = new_node
new_node.previous = curr
new_node.next = temp
temp.previous = new_node
self.size = self.size + 1
# Create instance and test
my_instance = DoublyLinkedList()
print("Elements are being added to the doubly linked list")
my_instance.add_data(10)
my_instance.add_data(24)
my_instance.add_data(54)
print("Elements are added to the middle of the list")
my_instance.add_data_in_middle(77)
my_instance.print_it()
my_instance.add_data_in_middle(92)
my_instance.print_it()
Elements are being added to the doubly linked list Elements are added to the middle of the list The nodes in the doubly linked list are: 10 24 77 54 The nodes in the doubly linked list are: 10 24 92 77 54
How It Works
The add_data_in_middle() method calculates the middle position and inserts the new node by:
- Finding the middle position using
size//2 - Traversing to that position
- Updating the
nextandpreviouspointers of adjacent nodes - Linking the new node between existing nodes
Key Points
| Operation | Time Complexity | Description |
|---|---|---|
| Insert at Middle | O(n) | Must traverse to middle position |
| Add at End | O(1) | Direct access via tail pointer |
| Print List | O(n) | Traverse all nodes |
Conclusion
Inserting at the middle of a doubly linked list requires calculating the position and updating four pointer connections. The bidirectional links allow efficient insertion while maintaining list integrity.
