Python Data Structures - Circular Doubly Linked List Other Related Topics

Python - Delete all nodes of the Circular Doubly Linked List



Deleting all nodes of a circular doubly linked list requires traverse through the list and deleting each node one by one. It requires creating a current node pointing to the next of head. Delete the current node and move to the next node using temp node. Repeat the process till the current node becomes head. At last, delete the head.

The function deleteAllNodes is created for this purpose. It is a 3-step process.

def deleteAllNodes(self):
  if self.head != None:
    
    #1. if head is not null create a temp node
    #   and current node pointed to next of head
    current = self.head.next
    while(current != self.head):
      
      #2. if current node is not equal to head, delete the
      #   current node and move current to next node using temp,
      #   repeat the process till the current reaches the head
      temp = current.next
      current = None
      current = temp
    
    #3. Delete the head
    self.head = None

  print("All nodes are deleted successfully.") 

The below is a complete program that uses above discussed concept of deleting all nodes of a circular doubly linked list which makes the list empty with size zero.

# node structure
class Node:
  def __init__(self, data):
    self.data = data
    self.next = None
    self.prev = None

#class Linked List
class LinkedList:
  def __init__(self):
    self.head = None

  #Add new element at the end of the list
  def push_back(self, newElement):
    newNode = Node(newElement)
    if(self.head == None):
      self.head = newNode
      newNode.next = self.head
      newNode.prev = self.head
      return
    else:
      temp = self.head
      while(temp.next != self.head):
        temp = temp.next
      temp.next = newNode
      newNode.next = self.head
      newNode.prev = temp
      self.head.prev = newNode

  #delete all nodes of the list
  def deleteAllNodes(self):
    if self.head != None:
      current = self.head.next
      while(current != self.head):
        temp = current.next
        current = None
        current = temp
      self.head = None
    print("All nodes are deleted successfully.") 

  #display the content of the list
  def PrintList(self):
    temp = self.head
    if(temp != None):
      print("The list contains:", end=" ")
      while (True):
        print(temp.data, end=" ")
        temp = temp.next
        if(temp == self.head):
          break
      print()
    else:
      print("The list is empty.")

# test the code                  
MyList = LinkedList()

#Add four elements in the list.
MyList.push_back(10)
MyList.push_back(20)
MyList.push_back(30)
MyList.push_back(40)

#Display the content of the list.
MyList.PrintList()

#delete all nodes of the list
MyList.deleteAllNodes()

#Display the content of the list.
MyList.PrintList()

The above code will give the following output:

The list contains: 10 20 30 40 
All nodes are deleted successfully.
The list is empty.