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

Created with Sketch.

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

In this program, we will create a doubly linked list and delete a node from the end of the list. If the list is empty, print the message “List is empty”. If the list is not empty, the tail’s previous node will become the new tail of the list thus, deleting the last node from the list.

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

In the above example, node new was the tail of the list. Make tail’s previous node that is, node 4 as the tail of the list. Node 4’s next will point to null.

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. deleteFromEnd() will delete a node from the end of the list:
  • It first checks whether the head is null (empty list) then, it will return from the function as there is no node present in the list.
  • If the list is not empty, it will check whether the list has only one node.
  • If the list has only one node, it will set both head and tail to null.
  • If the list has more than one node then, tail’s previous node will become the new tail of the list.
  • This new tail will point to null thus, delete the last node of the list.

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 DeleteEnd:
  8.     #Represent the head and tail of the doubly linked list  
  9.     def __init__(self):
  10.         self.head = None;
  11.         self.tail = None;
  12.     #addNode() will add a node to the list  
  13.     def addNode(self, data):
  14.         #Create a new node  
  15.         newNode = Node(data);
  16.         #If list is empty  
  17.         if(self.head == None):
  18.             #Both head and tail will point to newNode  
  19.             self.head = self.tail = newNode;
  20.             #head’s previous will point to None  
  21.             self.head.previous = None;
  22.             #tail’s next will point to None, as it is the last node of the list  
  23.             self.tail.next = None;
  24.         else:
  25.             #newNode will be added after tail such that tail’s next will point to newNode  
  26.             self.tail.next = newNode;
  27.             #newNode’s previous will point to tail  
  28.             newNode.previous = self.tail;
  29.             #newNode will become new tail  
  30.             self.tail = newNode;
  31.             #As it is last node, tail’s next will point to None  
  32.             self.tail.next = None;
  33.     #deleteFromEnd() will delete a node from the end of the list  
  34.     def deleteFromEnd(self):
  35.         #Checks whether list is empty  
  36.         if(self.head == None):
  37.             return;
  38.         else:
  39.             #Checks whether the list contains only one node  
  40.             if(self.head != self.tail):
  41.                 #Previous node to the tail will become new tail  
  42.                 self.tail = self.tail.previous;
  43.                 #Node next to current tail will be made None  
  44.                 self.tail.next = None;
  45.             #If the list contains only one element   
  46.             #Then it will remove the node, and now both head and tail will point to None  
  47.             else:
  48.                 self.head = self.tail = None;
  49.     #display() will print out the nodes of the list  
  50.     def display(self):
  51.         #Node current will point to head  
  52.         current = self.head;
  53.         if(self.head == None):
  54.             print(“List is empty”);
  55.             return;
  56.         while(current != None):
  57.             #Prints each node by incrementing pointer.  
  58.             print(current.data),
  59.             current = current.next;
  60.         print();
  61. dList = DeleteEnd();
  62. #Add nodes to the list  
  63. dList.addNode(1);
  64. dList.addNode(2);
  65. dList.addNode(3);
  66. dList.addNode(4);
  67. dList.addNode(5);
  68. #Printing original list  
  69. print(“Original List: “);
  70. dList.display();
  71. while(dList.head != None):
  72.     dList.deleteFromEnd();
  73.     #Printing updated list  
  74.     print(“Updated List: “);
  75.     dList.display();

Output:

Original List: 
1 2 3 4 5 
Updated List: 
1 2 3 4 
Updated List: 
1 2 3 
Updated List: 
1 2 
Updated List: 
1 
Updated List: 
List is empty

Leave a Reply

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