Deletion at different positions in a Circular Linked List | C # program to delete node at different positions 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
using System ; class GFG { class Node { public int data ; public Node next ; } ; static Node Insert ( Node head , int data ) { Node current = head ; Node newNode = new Node ( ) ; if ( newNode == null ) { Console . Write ( " STRNEWLINE Memory ▁ Error STRNEWLINE " ) ; return null ; } newNode . data = data ; if ( head == null ) { newNode . next = newNode ; head = newNode ; return head ; } else { while ( current . next != head ) { current = current . next ; } newNode . next = head ; current . next = newNode ; } return head ; } static void Display ( Node head ) { Node current = head ; if ( head == null ) { Console . Write ( " STRNEWLINE Display ▁ List ▁ is ▁ empty STRNEWLINE " ) ; return ; } else { do { Console . Write ( " { 0 } ▁ " , current . data ) ; current = current . next ; } while ( current != head ) ; } } static int Length ( Node head ) { Node current = head ; int count = 0 ; if ( head == null ) { return 0 ; } else { do { current = current . next ; count ++ ; } while ( current != head ) ; } return count ; } static Node DeleteFirst ( Node head ) { Node previous = head , next = head ; if ( head == null ) { Console . Write ( " List is " return null ; } if ( previous . next == previous ) { head = null ; return null ; } while ( previous . next != head ) { previous = previous . next ; next = previous . next ; } previous . next = next . next ; head = previous . next ; return head ; } static Node DeleteLast ( Node head ) { Node current = head , temp = head , previous = null ; if ( head == null ) { Console . Write ( " List is " return null ; } if ( current . next == current ) { head = null ; return null ; } while ( current . next != head ) { previous = current ; current = current . next ; } previous . next = current . next ; head = previous . next ; return head ; } static Node DeleteAtPosition ( Node head , int index ) { int len = Length ( head ) ; int count = 1 ; Node previous = head , next = head ; if ( head == null ) { Console . Write ( " STRNEWLINE Delete ▁ Last ▁ List ▁ is ▁ empty STRNEWLINE " ) ; return null ; } if ( index >= len index < 0 ) { Console . Write ( " Index is not " return null ; } if ( index == 0 ) { head = DeleteFirst ( head ) ; return head ; } while ( len > 0 ) { if ( index == count ) { previous . next = next . next ; return head ; } previous = previous . next ; next = previous . next ; len -- ; count ++ ; } return head ; } public static void Main ( String [ ] args ) { Node head = null ; head = Insert ( head , 99 ) ; head = Insert ( head , 11 ) ; head = Insert ( head , 22 ) ; head = Insert ( head , 33 ) ; head = Insert ( head , 44 ) ; head = Insert ( head , 55 ) ; head = Insert ( head , 66 ) ; Console . Write ( " Initial ▁ List : ▁ " ) ; Display ( head ) ; Console . Write ( " STRNEWLINE After ▁ Deleting ▁ node ▁ at ▁ index ▁ 4 : ▁ " ) ; head = DeleteAtPosition ( head , 4 ) ; Display ( head ) ; Console . Write ( " STRNEWLINE STRNEWLINE Initial ▁ List : ▁ " ) ; Display ( head ) ; Console . Write ( " STRNEWLINE After ▁ Deleting ▁ first ▁ node : ▁ " ) ; head = DeleteFirst ( head ) ; Display ( head ) ; Console . Write ( " STRNEWLINE STRNEWLINE Initial ▁ List : ▁ " ) ; Display ( head ) ; Console . Write ( " STRNEWLINE After ▁ Deleting ▁ last ▁ node : ▁ " ) ; head = DeleteLast ( head ) ; Display ( head ) ; } }