Delete all odd or even positioned nodes from Circular Linked List | C ++ program to delete all even and odd position nodes from Singly Circular Linked list ; structure for a node ; Function return number of nodes present in list ; if list is empty simply return length zero ; traverse forst to last node ; Function print data of list ; if list is empty simply show message ; traverse forst to last 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 firt node to last node ; put first or head node address in new node link ; put new node address into last node link ( next ) ; Utitlity function to delete a Node ; If node to be deleted is head node ; traverse list till not found delete node ; copy address of node ; Finally , free the memory occupied by del ; Function to 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 odd position nodes ; check list have any node if not then return ; if list have single node means odd position then delete it ; traverse first to last if list have more than one node ; delete first position node which is odd position ; Function to delete first node ; check position is odd or not if yes then delete node Note : Considered 1 based indexing ; Function to delete all even position nodes ; Take size of list ; Check list is empty if empty simply return ; if list have single node then return ; make first node is previous ; make second node is current ; check node number is even if node is even then delete that node ; Driver Code ; Deleting Odd positioned nodes ; Deleting Even positioned nodes
#include <bits/stdc++.h> NEW_LINE struct Node { int data ; struct Node * next ; } ; 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 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 ) ; } } 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 deleteNode ( struct Node * head_ref , struct Node * del ) { struct Node * temp = head_ref ; if ( head_ref == del ) { head_ref = del -> next ; } while ( temp -> next != del ) { temp = temp -> next ; } temp -> next = del -> next ; free ( del ) ; return ; } 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 DeleteAllOddNode ( struct Node * * head ) { int len = Length ( * head ) ; int count = 0 ; struct Node * previous = * head , * next = * head ; if ( * head == NULL ) { printf ( " Delete Last List is empty " return ; } if ( len == 1 ) { DeleteFirst ( head ) ; return ; } while ( len > 0 ) { if ( count == 0 ) { DeleteFirst ( head ) ; } if ( count % 2 == 0 && count != 0 ) { deleteNode ( * head , previous ) ; } previous = previous -> next ; next = previous -> next ; len -- ; count ++ ; } return ; } void DeleteAllEvenNode ( struct Node * * head ) { int len = Length ( * head ) ; int count = 1 ; struct Node * previous = * head , * next = * head ; if ( * head == NULL ) { printf ( " List is empty " return ; } if ( len < 2 ) { return ; } previous = * head ; next = previous -> next ; while ( len > 0 ) { if ( count % 2 == 0 ) { previous -> next = next -> next ; free ( next ) ; previous = next -> 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 Odd position nodes : " DeleteAllOddNode ( & head ) ; Display ( head ) ; printf ( " Initial List : " Display ( head ) ; printf ( " After deleting even position nodes : " DeleteAllEvenNode ( & head ) ; Display ( head ) ; return 0 ; }