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

python tutorials and learn python

Created with Sketch.

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

In this program, we will create a doubly linked list and delete a node from the beginning of the list. If the list is empty, print the message “List is empty”. If the list is not empty, we will make the head to point to the next node in the list then; we will delete the first node.

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

Consider the above example, new was the head of the list. Make head to point to next node in the list. Now, node 1 will become the new head of the list thus, deleting node new.

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. 2Define another class for creating a doubly linked list, and it has two nodes: head and tail. Initially, head and tail will point to null.
  3. 3deleteFromStart() will delete a node from the beginning 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, the head will point to the next node in the list and delete the old head node.

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 DeleteStart:
  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.     #deleteFromStart() will delete a node from the beginning of the list  
  34.     def deleteFromStart(self):
  35.         #Checks whether list is empty  
  36.         if(self.head == None):
  37.             return;
  38.         else:
  39.             #Checks whether the list contains only one element  
  40.             if(self.head != self.tail):
  41.                 #head will point to next node in the list  
  42.                 self.head = self.head.next;
  43.                 #Previous node to current head will be made None  
  44.                 self.head.previous = 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 = DeleteStart();
  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.deleteFromStart();
  73.     #Printing updated list  
  74.     print(“Updated List: “);
  75.     dList.display();

Output:

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

Leave a Reply

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