Deletion at different positions in a Circular Linked List | C ++ program to delete node at different poisitions from a circular linked list ; structure for a node ; Function to insert a node at the end of a Circular linked list ; Create a new node ; check node is created or not ; insert data into newly created node ; check list is empty if not have any node then make first node it ; if list have already some node ; move first node to last node ; put first or head node address in new node link ; put new node address into last node link ( next ) ; Function print data of list ; if list is empty , simply show message ; traverse first to last node ; Function return number of nodes present in list ; if list is empty simply return length zero ; traverse forst to last node ; Function delete First node of Circular Linked List ; check list have any node if not then return ; check list have single node if yes then delete it and return ; traverse second to first ; now previous is last node and next is first node of list first node ( next ) link address put in last node ( previous ) link ; make second node as head node ; Function to delete last node of Circular Linked List ; check if list doesn 't have any node  if not then return ; check if list have single node if yes then delete it and return ; move first node to last previous ; Function delete node at a given poisition of Circular Linked List ; Find length of list ; check list have any node if not then return ; given index is in list or not ; delete first node ; traverse first to last node ; if index found delete that node ; Driver Code ; Deleting Node at position ; Deleting first Node ; Deleting last Node
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * next ; } ; void Insert ( struct Node * * head , int data ) { struct Node * current = * head ; struct Node * newNode = new Node ; if ( ! newNode ) { printf ( " Memory Error " return ; } newNode -> data = data ; if ( * head == NULL ) { newNode -> next = newNode ; * head = newNode ; return ; } else { while ( current -> next != * head ) { current = current -> next ; } newNode -> next = * head ; current -> next = newNode ; } } void Display ( struct Node * head ) { struct Node * current = head ; if ( head == NULL ) { printf ( " Display List is empty " return ; } else { do { printf ( " % d ▁ " , current -> data ) ; current = current -> next ; } while ( current != head ) ; } } int Length ( struct Node * head ) { struct Node * current = head ; int count = 0 ; if ( head == NULL ) { return 0 ; } else { do { current = current -> next ; count ++ ; } while ( current != head ) ; } return count ; } void DeleteFirst ( struct Node * * head ) { struct Node * previous = * head , * next = * head ; if ( * head == NULL ) { printf ( " List is empty " return ; } if ( previous -> next == previous ) { * head = NULL ; return ; } while ( previous -> next != * head ) { previous = previous -> next ; next = previous -> next ; } previous -> next = next -> next ; * head = previous -> next ; free ( next ) ; return ; } void DeleteLast ( struct Node * * head ) { struct Node * current = * head , * temp = * head , * previous ; if ( * head == NULL ) { printf ( " List is empty " return ; } if ( current -> next == current ) { * head = NULL ; return ; } while ( current -> next != * head ) { previous = current ; current = current -> next ; } previous -> next = current -> next ; * head = previous -> next ; free ( current ) ; return ; } void DeleteAtPosition ( struct Node * * head , int index ) { int len = Length ( * head ) ; int count = 1 ; struct Node * previous = * head , * next = * head ; if ( * head == NULL ) { printf ( " Delete Last List is empty " return ; } if ( index >= len index < 0 ) { printf ( " Index is not Found " return ; } if ( index == 0 ) { DeleteFirst ( head ) ; return ; } while ( len > 0 ) { if ( index == count ) { previous -> next = next -> next ; free ( next ) ; return ; } previous = previous -> next ; next = previous -> next ; len -- ; count ++ ; } return ; } int main ( ) { struct Node * head = NULL ; Insert ( & head , 99 ) ; Insert ( & head , 11 ) ; Insert ( & head , 22 ) ; Insert ( & head , 33 ) ; Insert ( & head , 44 ) ; Insert ( & head , 55 ) ; Insert ( & head , 66 ) ; printf ( " Initial ▁ List : ▁ " ) ; Display ( head ) ; printf ( " After Deleting node at index 4 : " DeleteAtPosition ( & head , 4 ) ; Display ( head ) ; printf ( " Initial List : " Display ( head ) ; printf ( " After Deleting first node : " DeleteFirst ( & head ) ; Display ( head ) ; printf ( " Initial List : " Display ( head ) ; printf ( " After Deleting last node : " DeleteLast ( & head ) ; Display ( head ) ; return 0 ; }