
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Insert New Node at the Middle of Doubly Linked List in Python
When it is required to insert a new node in the middle 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 −
Example
class Node: def __init__(self, my_data): self.previous = None self.data = my_data self.next = None class double_list: 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; temp.previous = curr; curr.next = new_node; new_node.previous = curr; new_node.next = temp; temp.previous = new_node; self.size = self.size + 1; my_instance = double_list() 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()
Output
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
Explanation
- The ‘Node’ class is created.
- Another class with required attributes is created.
- A method named ‘add_data_in_middle’ is defined, that is used to add data in the middle index of the doubly linked list.
- A method named ‘add_data’ is defined, that helps add nodes to the doubly linked list.
- Another method named ‘print_it’ is defined, that displays the nodes of the circular linked list.
- An object of the ‘double_list’ class is created, and the methods are called on it to add data to the doubly linked list.
- The ‘add_data_in_middle’ is called to add data to middle index of the linked list.
- An ‘init’ method is defined, that the root, head, and tail nodes of the doubly linked list to None.
- This is displayed on the console using the ‘print_it’ method.
Advertisements