To perform

###

let's say we have a list that has four nodes in it.

to delete the first node of the list we simply store the second node's reference into the last node's link part.

so now the first node is deleted from the linked list.

###

if we have a linked list that has only one node.

then to delete this only node we simply store None or Null value into the last variable.

so now our list becomes empty.

###

to delete the last node first we need to find a reference p of the predecessor of the last node.

then we store the first node's reference into the linked part of node p.

after that, we update the list variable's position. because after deletion of the last node now the node p becomes the last node of the list.

so now after performing these operations the last node's is deleted.

###

to delete a node at position x. first, we need to find the reference to the predecessor of the node. like we want to delete the third node on the list. then we need to find the reference p of the second node.

after that, we store the node's reference that comes after the node that we want to delete in the linked part of node p.

so now the third node's deleted from the linked list.

####

**deletion**in a**circular linked list**we need to maintain and store the reference of the last node of the list. because using the reference of the last node the deletion operation can be done in constant time.**so in this tutorial, we are going to learn how to**- Delete the first node of the list.
- Delete the only node of the list.
- Delete the last node of the list.
- Delete a node at any position in the list.

###
__Delete the first node of the circular linked list.__

let's say we have a list that has four nodes in it.to delete the first node of the list we simply store the second node's reference into the last node's link part.

so now the first node is deleted from the linked list.

###
__Delete the only node of the circular linked list.__

if we have a linked list that has only one node.then to delete this only node we simply store None or Null value into the last variable.

so now our list becomes empty.

###
__Delete the last node of the circular linked list.__

to delete the last node first we need to find a reference p of the predecessor of the last node.then we store the first node's reference into the linked part of node p.

after that, we update the list variable's position. because after deletion of the last node now the node p becomes the last node of the list.

so now after performing these operations the last node's is deleted.

###
__Delete a node at the position in a circular linked list.__

to delete a node at position x. first, we need to find the reference to the predecessor of the node. like we want to delete the third node on the list. then we need to find the reference p of the second node.after that, we store the node's reference that comes after the node that we want to delete in the linked part of node p.

so now the third node's deleted from the linked list.

####
__Python program code for performing deletion in the circular linked list.__

class Node(object): def __init__(self, value): self.info = value self.link = None class CircularLinkedList(object): def __init__(self): self.last = None def display_list(self): if self.last == None: print("List is empty\n") return p = self.last.link while True: print(p.info, " ", end='') p = p.link if p == self.last.link: break print() def insert_in_beginning(self, data): temp = Node(data) temp.link = self.last.link self.last.link = temp def insert_in_empty_list(self, data): temp = Node(data) self.last = temp self.last.link = self.last def insert_at_end(self,data): temp = Node(data) temp.link = self.last.link self.last.link = temp self.last = temp def create_list(self): n = int(input("Enter the number of nodes : ")) if n == 0: return data = int(input("Enter the element to be inserted : ")) self.insert_in_empty_list(data) for i in range(n-1): data = int(input("Enter the element to be inserted : ")) self.insert_at_end(data) def insert_after(self, data, x): p = self.last.link while True: if p.info == x: break p = p.link if p == self.last.link: break if p == self.last.link and p.info != x: print(x, " not present in the list") else: temp = Node(data) temp.link = p.link p.link = temp if p == self.last: self.last = temp def delete_first_node(self): if self.last is None: return if self.last.link == self.last: self.last = None return self.last.link = self.last.link.link def delete_last_node(self): if self.last is None: return if self.last.link == self.last: self.last = None return p = self.last.link while p.link != self.last: p = p.link p.link = self.last.link self.last = p def delete_node(self,x): if self.last is None: return if self.last.link == self.last and self.last.info == x: self.last = None return if self.last.link.info == x: self.last.link == self.last.link.link return p = self.last.link while p.link != self.last.link: if p.link.info == x: break p = p.link if p.link == self.last.link: print(x, " not found in the list") else: p.link = p.link.link if self.last.info == x: self.last = p ################################################# list = CircularLinkedList() list.create_list() while True: print("1. Display list") print("2. Insert in empty list") print("3. Insert a node in the beginning of the list") print("4. Insert a node at the end of the list") print("5. Insert a node after a specified node") print("6. Delete first node") print("7. Delete last node") print("8. Delete any node") print("9. Quite") option = int(input("Enter your choice : ")) if option == 1: list.display_list() elif option == 2: data = int(input("Enter the element to be inserted : ")) list.insert_in_empty_list(data) elif option == 3: data = int(input("Enter the element to be inserted : ")) list.insert_in_beginning(data) elif option == 4: data = int(input("Enter the element to be inserted : ")) list.insert_at_end(data) elif option == 5: data = int(input("Enter the element to be inserted : ")) x = int(input("Enter the element after which to insert : ")) list.insert_after(data,x) elif option == 6: list.delete_first_node() elif option == 7: list.delete_last_node() elif option == 8: data = int(input("Enter the element to be deleted : ")) list.delete_node(data) elif option == 9: break else: print("Wrong option") print()

#### Also, read these posts

- What are Data Structures and algorithms
- Algorithm design and analysis
- Classification of algorithms
- How to calculate the running time of an algorithm.
- Worst Average and Best-case analysis of the algorithm.
- Big o notation
- Big o notation examples
- Linked List in Data Structures
- Traversing in Linked list
- Operations on the linked list
- Insertion in the linked list
- Deletion in a linked list
- Reversing a linked list
- Sorting a linked list
- Find and remove the loop in the linked list
- Doubly linked list
- Insertion in the doubly linked list
- Deletion in the doubly linked list
- Reversing a doubly linked list
- Circular linked list
- Insertion in the circular linked list
- Merge two linked list
- Header linked list
- Sorted linked list
- Stack in data structures
- Queue in data structures
- Circular queue
- Dequeue in the data structure
- Priority queue
- Polish notation
- Tree in the data structure
- Binary tree
- Array representation of the binary tree
- linked representation of a binary tree
- Traversing in the binary tree
- Inorder traversal in the binary tree
- Preorder traversal in the binary tree
- Postorder traversal in the binary tree
- Level order traversal in the binary tree
- Binary search tree
- Insertion in the binary search tree
- Deletion in the binary search tree
- Heap in data structures

- What are Data Structures and algorithms
- Algorithm design and analysis
- Classification of algorithms
- How to calculate the running time of an algorithm.
- Worst Average and Best-case analysis of the algorithm.
- Big o notation
- Big o notation examples
- Linked List in Data Structures
- Traversing in Linked list
- Operations on the linked list
- Insertion in the linked list
- Deletion in a linked list
- Reversing a linked list
- Sorting a linked list
- Find and remove the loop in the linked list
- Doubly linked list
- Insertion in the doubly linked list
- Deletion in the doubly linked list
- Reversing a doubly linked list
- Circular linked list
- Insertion in the circular linked list
- Merge two linked list
- Header linked list
- Sorted linked list
- Stack in data structures
- Queue in data structures
- Circular queue
- Dequeue in the data structure
- Priority queue
- Polish notation
- Tree in the data structure
- Binary tree
- Array representation of the binary tree
- linked representation of a binary tree
- Traversing in the binary tree
- Inorder traversal in the binary tree
- Preorder traversal in the binary tree
- Postorder traversal in the binary tree
- Level order traversal in the binary tree
- Binary search tree
- Insertion in the binary search tree
- Deletion in the binary search tree
- Heap in data structures

## 0 Comments