Deletion from end in circular linked list in C++ is one of the deletion operation that we can perform on the circular sequenced list. The other deletion operations that we can perform are:-
Deletion from beginning i.e. to delete the head node of the list.
Deletion from any specific position i.e. to delete any node from the list.
Steps to delete from end in circular linked list in CPP
Print deletion not possible if list is empty.
Else define two node pointers cur and prev
Initialize cur pointer with head
Change prev pointer to current and cur to cur->next until next of the current does not equal to head.
Now link last second node that is the node pointed by prev node pointer to head.
Free cur as it stores the last node of the list.
structNode{int num;structNode*next;};
Algorithm to write a CPP program to delete from end in Circular Linked List
#include<iostream>
using namespace std;
struct Node
{
int num;
struct Node *next;
} *head;
void insertStart (struct Node **head, int data) //function to create linked list
{
struct Node *newNode = (struct Node *) malloc (sizeof (struct Node));
newNode->num = data;
// if its the first node being entered
if (*head == NULL)
{
*head = newNode; // assigning itself as head
(*head)->next = *head; // assigning itself as next node
cout << newNode->num << " Inserted\n";
return;
}
// if CLL already as >=1 node
struct Node *curr = *head;
// traverse till last node in CLL
while (curr->next != *head)
{
curr = curr->next;
}
curr->next = newNode; // last node's next as this new node
newNode->next = *head; // new node's next as current head node
*head = newNode; // changing head node to this new node
cout << newNode->num << " Inserted\n";
// previous head node becomes 2nd node
}
void deleteEnd (struct Node **head) //fuction to delete last node from the circular linked list
{
struct Node *cur, *prev;
cur = (*head);
while (cur->next != *head)
{
prev = cur;
cur = cur->next;
}
prev->next = *head;
free (cur);
}
void display (struct Node *head)
{
cout << "\nCircular Linked List : " << endl;
// if circular linked list is empty currently
if (head == NULL)
return;
struct Node *temp = head;
// since we need to take care of circular nature of linked list
do
{
cout << temp->num << " ";
temp = temp->next;
}
while (temp != head);
cout << endl;
}
int main () //main function
{
// first node will be null at creation
struct Node *head = NULL;
insertStart (&head, 1);
insertStart (&head, 2);
insertStart (&head, 3);
insertStart (&head, 4);
insertStart (&head, 5);
cout << "Before insertion ";
display (head);
deleteEnd (&head);
cout << "After insertion ";
display (head);
return 0;
}
Output:
Enter the size of circular linked list: 6
Enter data of the list:
11
21
31
41
51
61
Circular linked list data:
11 21 31 41 51 61
After deleting last node, new list is:
Circular linked list data:
11 21 31 41 51
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Login/Signup to comment