Python program to insert a new node at the middle of the Doubly Linked List.

python tutorials and learn python

Created with Sketch.

Python program to insert a new node at the middle of the Doubly Linked List.

In this program, we create a doubly linked list and insert a new node in the middle of the list. If the list is empty, both head and tail will point to the new node. If the list is not empty, then we will calculate the size of the list and divide it by 2 to get the mid-point of the list where a new node needs to be inserted.

Python program to insert a new node at the middle of the Doubly Linked List

Consider the above diagram; a new node needs to be added to the middle of the list. First, we calculate the size which in this case is 4. So, to get the mid-point, we divide it by 2 and store it in a variable mid. Node current will point to head. First, we iterate through the list until current points to the mid position. Define another node temp which point to node next to current. Insert the new node between current and temp

ALGORITHM:

  1. Define a Node class which represents a node in the list. It will have three properties: data, previous which will point to the previous node and next which will point to the next node.
  2. Define another class for creating the doubly linked list, and it has two nodes: head and tail. Initially, head and tail will point to null.
  3. addNode() will add node to the list:
  • It first checks whether the head is null, then it will insert the node as the head.
  • Both head and tail will point to a newly added node.
  • Head’s previous pointer will point to null and tail’s next pointer will point to null.
  • If the head is not null, the new node will be inserted at the end of the list such that new node’s previous pointer will point to tail.
  • The new node will become the new tail. Tail’s next pointer will point to null.

a. addInMid() will add a node to the middle of the list:

  • It first checks whether the head is null (empty list), then it will insert the node as the head.
  • Both head and tail will point to a newly added node.
  • If the list is not empty, then we calculate size and divide it by 2 to get the mid-point.
  • Define node current that will point to head and iterate through the list till current will point to the mid node.
  • Define another node temp which will point to node next to current.
  • The new node will be inserted after current and before temp such that current will point to the new node and the new node will point to temp.

a. display() will show all the nodes present in the list.

  • Define a new node ‘current’ that will point to the head.
  • Print current.data till current points to null.
  • Current will point to the next node in the list in each iteration.

PROGRAM:

  1. #Represent a node of doubly linked list  
  2. class Node:
  3.     def __init__(self,data):
  4.         self.data = data;
  5.         self.previous = None;
  6.         self.next = None;
  7. class InsertMid:
  8.     #Represent the head and tail of the doubly linked list  
  9.     def __init__(self):
  10.         self.head = None;
  11.         self.tail = None;
  12.         self.size = 0;
  13.     #addNode() will add a node to the list  
  14.     def addNode(self, data):
  15.         #Create a new node  
  16.         newNode = Node(data);
  17.         #If list is empty  
  18.         if(self.head == None):
  19.             #Both head and tail will point to newNode  
  20.             self.head = self.tail = newNode;
  21.             #head’s previous will point to None  
  22.             self.head.previous = None;
  23.             #tail’s next will point to None, as it is the last node of the list  
  24.             self.tail.next = None;
  25.         else:
  26.             #newNode will be added after tail such that tail’s next will point to newNode  
  27.             self.tail.next = newNode;
  28.             #newNode’s previous will point to tail  
  29.             newNode.previous = self.tail;
  30.             #newNode will become new tail  
  31.             self.tail = newNode;
  32.             #As it is last node, tail’s next will point to None  
  33.             self.tail.next = None;
  34.         #Size will count the number of nodes present in the list  
  35.         self.size = self.size + 1;
  36.     #addInMid() will add a node to the middle of the list  
  37.     def addInMid(self, data):
  38.         #Create a new node  
  39.         newNode = Node(data);
  40.         #If list is empty  
  41.         if(self.head == None):
  42.             #Both head and tail will point to newNode  
  43.             self.head = self.tail = newNode;
  44.             #head’s previous will point to None  
  45.             self.head.previous = None;
  46.             #tail’s next point to None, as it is the last node of the list  
  47.             self.tail.next = None;
  48.         else:
  49.             #current will point to head  
  50.             current = self.head;
  51.             #Store the mid position of the list  
  52.             mid = (self.size//2if(self.size % 2 == 0)  else ((self.size+1)//2);
  53.             #Iterate through list till current points to mid position  
  54.             for i in range(1, mid):
  55.                 current = current.next;
  56.             #Node temp will point to node next to current  
  57.             temp = current.next;
  58.             temp.previous = current;
  59.             #newNode will be added between current and temp  
  60.             current.next = newNode;
  61.             newNode.previous = current;
  62.             newNode.next = temp;
  63.             temp.previous = newNode;
  64.         self.size = self.size + 1;
  65.     #display() will print out the nodes of the list  
  66.     def display(self):
  67.         #Node current will point to head  
  68.         current = self.head;
  69.         if(self.head == None):
  70.             print(“List is empty”);
  71.             return;
  72.         while(current != None):
  73.             #Prints each node by incrementing pointer.  
  74.             print(current.data),
  75.             current = current.next;
  76.         print();
  77. dList = InsertMid();
  78. #Add nodes to the list  
  79. dList.addNode(1);
  80. dList.addNode(2);
  81. print(“Original list: “);
  82. dList.display();
  83. #Adding node ‘3’ in the middle  
  84. dList.addInMid(3);
  85. print“Updated List: “);
  86. dList.display();
  87. #Adding node ‘4’ in the middle  
  88. dList.addInMid(4);
  89. print(“Updated List: “);
  90. dList.display();
  91. #Adding node ‘5’ in the middle  
  92. dList.addInMid(5);
  93. print(“Updated List: “);
  94. dList.display();

Output:

Original list: 
1 2 
Updated List: 
1 3 2 
Updated List: 
1 3 4 2 
Updated List: 
1 3 5 4 2

Leave a Reply

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