Python program to delete a new node from the middle of the doubly linked list


When it is required to delete a node from 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.

Another class needs to be created that would have an initialization function, and the head of the node would be initialized to ‘None’ inside this.

Multiple methods are defined by the user to add node to the linked list, to display the nodes and to delete the node from the end of the doubly linked list.

In a doubly linked list, the nodes have pointers. The current node would have a pointer to the next node as well as the previous node. The last value in the list will have ‘NULL’ value in the next pointer. It can be traversed in both the directions.

Below is a demonstration for the same −

Example

 Live Demo

class Node:
   def __init__(self, my_data):
      self.prev = 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;
   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 delete_from_middle(self):
      if(self.head == None):
         return;
      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;
         if(curr == self.head):
            self.head = curr.next
            self.tail.next = None
         elif(curr == self.tail):
            self.tail = self.tail.prev;
         else:
            curr.prev.next = curr.next;
            curr.next.prev = curr.prev;
         current = None;
      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)
my_instance.add_data(77)
my_instance.add_data(92)
my_instance.print_it()
while(my_instance.head != None):
my_instance.delete_from_middle();
print("The list after deleting the element from the middle is : ")
my_instance.print_it();

Output

Elements are being added to the doubly linked list
The nodes in the doubly linked list are :
10
24
54
77
92
The list after deleting the element from the middle is :
The nodes in the doubly linked list are :
24
54
77
92
The list after deleting the element from the middle is :
The nodes in the doubly linked list are :
54
77
92
The list after deleting the element from the middle is :
The nodes in the doubly linked list are :
77
92
The list after deleting the element from the middle is :
The nodes in the doubly linked list are :
92
The list after deleting the element from the middle is :
The list is empty

Explanation

  • The ‘Node’ class is created.
  • Another class with required attributes is created.
  • A method named ‘add_data’ is defined, that is used to add data to the doubly linked list.
  • Another method named ‘print_it’ is defined, that displays the nodes of the circular linked list.
  • Another method named ‘delete_from_middle’ is defined, that deletes the node from the middle, and makes the previous node as the current node in the circular linked list.
  • An object of the ‘double_list’ class is created, and the methods are called on it to delete a node from the beginning of the doubly linked list.
  • An ‘init’ method is defined, that the root, head, and tail nodes of the doubly linked list to None.
  • The list is iterated over, and every node from the middle index of the linked list is deleted until its empty.
  • This is displayed on the console using the ‘print_it’ method.

Updated on: 11-Mar-2021

224 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements