C Program for Deletion from end in Circular Linked List

Deletion at end in Circular Linked List using C

Deleting node from LAST in circular linked list

In this section we will learn C Program for Deletion from end in Circular Linked List. Removing end node from the linked list means replacing the pointer of the second last node with the pointer of first node in circular sequenced list.

For Example :- Input : 19 5 2 110
Output : 19 5 2 (delete node from the last)

Algorithm for deleting node from end in the circular linked list :-

  • Step 1:   IF HEAD = NULL ( GO TO STEP 8 )
  • Step 2:   NOW SET POINTER = HEAD
  • Step 3:   REPEAT STEP 4 AND 5 UNTIL POINTER → NEXT != HEAD
  • Step 4:   ASSIGN PRE_POINTER = POINTER
  • Step 5:   ASSIGN POINTER = POINTER → NEXT
  • Step 6:   ASSIGN PRE_POINTER → NEXT = HEAD
  • Step 7:   REMOVE OR FREE POINTER
  • Step 8:   EXIT

Syntax :-

     struct node
 {
     int data; 
     struct node* next;  
 };

Construction of node in circular linked list :-

C Program for deletion from end in Circular Linked List

Working needed C Program for Deletion from end in Circular Linked List:-

  • CASE 1 :- If the linked list is empty then head == NULL and exit.
  • CASE 2 :- If the linked  list have only single node then, head → next == head. In this scenario, we need to delete the list and make the head pointer free.
  • CASE 3 :- If the linked list contains more than one node then,  we need to move whole linked list by using the pointer to reach the last node of the list.
  • We need to keep the second last node of the list.
  • Then we need to make the two pointers pointer and prepointer.
  • After that we need to make one more next pointer of prepointer point to the next of pointer(head node)
  • Last make pointer free.

To learn click below :-

C Program for deletion from in cirular linked list
#include <stdio.h>
#include <stdlib.h>

struct node        //structure of node
{
  int data;
  struct node *nextpt;
} *headpt;

void delete_end_node ()		//function for deleting end node in the linked list
{
  struct node *n,*r;
  n = headpt;
  while (n->nextpt != headpt)
    {
      r=n;     
      n = n->nextpt;
    }
  r->nextpt = headpt;
  printf ("The deletion at end node is -> %d", n->data);
  free (n);
}

void list (int n)			// list creation function of circular linked list
{
  int l, data;
  struct node *prevpt, *new_node;

  if (n >= 1)
    {
      headpt = (struct node *) malloc (sizeof (struct node));

      printf ("Enter data for node 1 : ");
      scanf ("%d", &data);
      headpt->data = data;
      headpt->nextpt = NULL;
      prevpt = headpt;
      for (l = 2; l <= n; l++)
	{
	  new_node = (struct node *) malloc (sizeof (struct node));
	  printf ("Enter data for node %d : ", l);
	  scanf ("%d", &data);
	  new_node->data = data;
	  new_node->nextpt = NULL;
	  prevpt->nextpt = new_node;
	  prevpt = new_node;
	}
      prevpt->nextpt = headpt;
    }
}

void print (int m)			// print function of circular linked list
{
  struct node *extra;
  int n = 1;

  if (headpt == NULL)
    {
      printf ("The List is empty");
    }
  else
    {
      extra = headpt;
      if (m == 1)
	{
	  printf ("The circular linked list is :\n");
	}
      else
	{
	  printf ("After insertion the list is :\n");
	}
      do
	{
	  printf ("The data at node %d is %d\n", n, extra->data);
	  extra = extra->nextpt;
	  n++;
	}
      while (extra != headpt);
    }
}

int main ()
{
  int value, n, x;
  printf ("Enter the size of nodes: \n");
  scanf ("%d", &n);
  list (n);
  print (x);
  delete_end_node (value);
  return 0;
}
Output :-
Enter the size of nodes: 
5
Enter data for node 1 : 6
Enter data for node 2 : 74
Enter data for node 3 : 1
Enter data for node 4 : 30
Enter data for node 5 : 19
After insertion the list is :
The data at node 1 is 6
The data at node 2 is 7
The data at node 3 is 1
The data at node 4 is 30
The data at node 5 is 19
The deletion at end node is -> 19