Circular Linked List Deletion

Circular Linked List Deletion

use code from the create and display nodes and getnode function to assign data click here

  1. create a function deletebeg( )
  2. create a last,temp pointer
Circular Linked List Deletion

3.intialize head pointer to last, temp

Circular Linked List Deletion

4.move last pointer to the last node ,by replacing last pointer with next node address (last = last -> next) until the condition (last -> next != head)

Circular Linked List Deletion

5.move head pointer to its next node(head=head->next)

Circular Linked List Deletion

6.link next address of last node with head node(last->next=head)

Circular Linked List Deletion

7.delete temp node using free kewyword in c

Circular Linked List Deletion

8.first node will be deleted

Circular Linked List Deletion

code to delete node from beginning of a circular linked list

void clldeletebeg()
{
node *temp, *last;
if(start == NULL)
{
printf("\n No nodes exist..");
getch();
return ;
}
else
{
last = temp = start;
while(last -> next != start)
last = last -> next;
head = start -> next;
last -> next = head;
free(temp);
printf("\n Node deleted..");
}
}

delete node from ending of a Circular linked list

use code from the create and display nodes and getnode function to assign data click here

  1. create a function deleteend( )
  2. create a prev,temp pointer

3.intilaze head to prev,temp pointers

Circular Linked List Deletion

\4. create loop with condition (temp->next!=head)

inside the loop initialize temp to prev and move temp pointer to next node using the next address field, we are using while loop to move prev pointer to the second element from the ending and temp pointer to the last node

Circular Linked List Deletion

5.link next address of prev node with head node

Circular Linked List Deletion

6.delete temp node using free keyword in c

Circular Linked List Deletion

7.last node will be deleted

Circular Linked List Deletion

code to delete node from ending of a Circular linked list

void cll_delete_last()
{
node *temp,*prev;
if(head == NULL)
{
printf("\n No nodes exist..");
getch();
return ;
}
else
{
temp = head ;
prev = head;
while(temp -> next != head)
{
prev = temp;
temp = temp -> next;
}
prev -> next = head;
free(temp);
printf("\n Node deleted..");
}
}
Last updated on by vishal devxo