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
Selected Reading
Python program to insert a new node at the end of the Doubly Linked List
When inserting a new node at the end of a doubly linked list, we need to create a Node class that contains data and references to both the previous and next nodes. The doubly linked list maintains pointers to both the head and tail for efficient insertion.
Node Structure
A doubly linked list node contains three components ?
Implementation
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_end(self, my_data):
new_node = Node(my_data)
# If list is empty, new node becomes both head and tail
if self.head is None:
self.head = self.tail = new_node
self.head.prev = None
self.tail.next = None
else:
# Link the new node to the current tail
self.tail.next = new_node
new_node.prev = self.tail
self.tail = new_node
self.tail.next = None
def print_list(self):
current = self.head
if self.head is None:
print("The list is empty")
return
print("The nodes in the doubly linked list are:")
while current is not None:
print(current.data)
current = current.next
# Create instance and test
dll = DoublyLinkedList()
print("Elements are being added to the end of doubly linked list")
dll.add_data_at_end(10)
dll.print_list()
dll.add_data_at_end(24)
dll.print_list()
dll.add_data_at_end(54)
dll.print_list()
dll.add_data_at_end(77)
dll.print_list()
dll.add_data_at_end(92)
dll.print_list()
Elements are being added to the end of doubly linked list The nodes in the doubly linked list are: 10 The nodes in the doubly linked list are: 10 24 The nodes in the doubly linked list are: 10 24 54 The nodes in the doubly linked list are: 10 24 54 77 The nodes in the doubly linked list are: 10 24 54 77 92
How It Works
The insertion process follows these steps ?
- Create new node: Initialize a new node with the given data
- Check if empty: If the list is empty, the new node becomes both head and tail
- Link nodes: Connect the current tail's next pointer to the new node
- Update references: Set the new node's previous pointer to the current tail
- Update tail: Make the new node the new tail of the list
Key Points
- The
Nodeclass containsprev,data, andnextattributes - The
DoublyLinkedListclass maintains bothheadandtailpointers - When the list is empty, the new node becomes both head and tail
- For non-empty lists, we update the tail's next pointer and the new node's prev pointer
- Time complexity for insertion at the end is O(1) due to the tail pointer
Conclusion
Inserting at the end of a doubly linked list is efficient with O(1) time complexity when maintaining a tail pointer. The key is properly linking the previous and next pointers to maintain the bidirectional structure.
Advertisements
