Circular Linked List Deletion

Last Updated on

Circular Linked List Deletion

Delete first node

Circular Linked List Deletion

import 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 1

3.intialize head pointer to last, temp

Circular Linked List Deletion 2

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 3

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

Circular Linked List Deletion 4

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

Circular Linked List Deletion 5

7.delete temp node using free kewyword in c

Circular Linked List Deletion 6

8.first node will be deleted

Circular Linked List Deletion 7

Program to Delete first node

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 last node

import 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 8

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 9

5.link next address of prev node with head node

Circular Linked List Deletion 10

6.delete temp node using free keyword in c

Delete last node

7.last node will be deleted

Circular Linked List Deletion 11

Program to Delete last node

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..");
	}
}