Deletion in Circular Linked List in C++
What is deletion in circular linked in CPP programming?
Deletion in circular linked in C++ in general words can be understood as deleting a node from a predefined circular linked list. We can perform three different types of deletion in circular linked list, these different types of deletion are:-
- Deletion from beginning of the circular linked list.
- Deletion from specific position in circular linked list.
- Deletion from end in circular linked list.
Types of deletion in circular linked list
1.) Deletion from beginning:
In this type of deletion in circular linked list delete the head of the list and the second node of the list act as new head.
2.) Deletion from specific position:
If we want to delete a node from specific position then we need to traverse till that position and delete that node. After deleting the node we will decrease the position of nodes after that node by one.
3.) Deletion from end:
In this of deletion in circular linked list we delete the tail of the list and after deleting the tail we will make last second node as the the new tail of the list
How to perform deletion from beginning in circular linked list?
To perform deletion from beginning in circular linked list we will use the following steps:-- We will create a circular linked list with some data in it.
- We will define a node pointer and this node pointer as head of the list.
- Now we will traverse this node pointer till the end of the list and follow the algorithm for deletion from beginning in circular linked list below.
void deleteBegin (struct Node **head) //function to delete beginning node from the circular linked list { struct Node *p, *temp; p = *head; while (p->next != (*head)) { p = p->next; } temp = *head; p->next = (*head)->next; *head = (*head)->next; free (temp); }
How to perform deletion from specific position in circular linked list?
To perform deletion from specific position in circular linked list we will use the following steps:-- We will create a circular linked list.
- We will use a node pointer to traverse till the specific node.
- Using a second pointer we will store the node there temporary and will link the previous node to the next of this node.
- At last we will free the temporary pointer.
void deleteSpecific(int pos) { struct node *p,*q; int del,k=0; del=pos-1; p=head; while(k!=del) { q=p; p=p->next; k++; } q->next=p->next; free(p);//deleting specific node }
How to perform deletion from end in circular linked list?
To perform deletion from end in circular linked list we will use the following steps:- After creating a linked list with some data in it we will define:-- We will define a node pointer this will initially point the head of the list and later will be used for traversing till the end of the list
- After traversing till the end of list we will store the last node in a temporary node.
- Now we will link the pointer of last second node to the head node.
- And at last we will free the node stored in a temporary node.
void deleteEnd() { struct node *cur,*prev; cur=head; while(cur->next!=head) { prev=cur; cur=cur->next; } prev->next=head; free(cur); }
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Circular Linked List
- Introduction to Circular Linked List
Click Here - Circular Linked List Applications
Click Here - Circular Linked List in –
- Insertion in Circular Linked List –
- Insertion at the beginning–
- Insertion at the end –
- Insertion at nth position –
- Deletion in Circular Linked List –
- Deletion from beginning in Circular Linked List –
- Deletion from nth position in Circular Linked List –
- Deletion from end in Circular Linked List –
- Insertion and Deletion in Circular Linked List – C | C++ | Java
- Split a Circular Linked List in two halves –
- Count nodes in Circular Linked List –
- Sorted Insert In Circular Linked List –
- Insertion in the middle in Circular Linked List –
Circular Linked List
- Introduction to Circular Linked List
- Circular Linked List Applications
- Circular Linked List in – C | C++ | Java
- Insertion in Circular Linked List – C | C++ | Java
- Deletion in Circular Linked List – C | C++ | Java
- Insertion and Deletion in a Circular Linked List – C | C++ | Java
- Split a Circular Linked List in two halves – C | C++ | Java
- Count nodes in Circular Linked List – C | C++ | Java
- Sorted Insert In Circular Linked List – C | C++ | Java
- Insertion in the middle in Circular Linked List – C | C++ | Java
Login/Signup to comment