Python - Delete the first node of the Circular Doubly Linked List

In this method, the first node of the circular doubly linked list is deleted. For example - if the given list is 10->20->30->40 and the first node is deleted, the list becomes 20->30->40.

Deleting the first node of the circular doubly linked list is very easy. If the list contains one node, delete the node. If the list contains more than one node, then create two nodes - temp and firstNode both pointing to the head. Using temp node, traverse to the last node of the list. Make next of head as the head node and update all links. Finally, delete the first node.

The function pop_front is created for this purpose. It is a 4-step process.

```def pop_front(self):

#1. the list contains one node, delete
else:

#2. if the list contains more than one node,
#   create two nodes - temp and firstNode, both

#3. using temp node, traverse to the last node
temp = temp.next

firstNode = None
```

The below is a complete program that uses above discussed concept of deleting the first node of the circular doubly linked list.

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

def __init__(self):

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

#Delete first node of the list
def pop_front(self):
else:
temp = temp.next
firstNode = None

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

# test the code

#Add three elements at the end of the list.
MyList.push_back(10)
MyList.push_back(20)
MyList.push_back(30)
MyList.push_back(40)
MyList.PrintList()

#Delete the first node
MyList.pop_front()
MyList.PrintList()
```

The above code will give the following output:

```The list contains: 10 20 30 40
The list contains: 20 30 40
```

5