C Program for Insertion at the end in circular linked list

Insertion at end in Circular Linked List in C

Inserting a new node at the last in circular linked list

Today we will learn how to write a code in C Program for Insertion at the end in circular linked list. For doing so, we will keep the following things in mind.

  • If the linked list is blank or empty, then head and tail will point to the newly added node.
  • If the linked list is not empty, then the newly inserted node will become the new tail of the circular linked list.

In other words, the new node will become last node (tail) of the list, and the previous tail will be the second last node.

Algorithm for adding new node at the end of the circular linked list :-

  • Step 1: IF HEAD = NULL (END OF IF AND GO TO STEP 8)
  • Step 2: ASSIGN POINTER = HEAD
  • Step 3: WHILE POINTER -> NEXT != HEAD (REPEAT STEP 4 AND 5)
  • Step 4: ASSIGN PREVIOUS_POINTER = POINTER
  • Step 5: ASSIGN POINTER = POINTER -> NEXT (END OF WHILE LOOP)
  • Step 6: ASSIGN PREVIOUS_POINTER -> NEXT = HEAD
  • Step 7: REMOVE POINTER
  • Step 8: EXIT

Working require for inserting node at the last of the circular linked list :-

  • Make a new node.
  • Assign the new node next to circular list.
  • If the list is empty then return new node.
  • Assign the new node next to the front of the list.
  • Assign tail next to the new node.
  • Return the end node of the circular linked list.

Structure of node in circular linked list :-

Code :-

     struct node
 {
     int data; 
     struct node* next;  
 };
C program for insertion at end in Circular linked list

C Program for Insertion at the end in circular linked list :-

#include <stdio.h>
#include <stdlib.h>
struct node    //structure of node in circular
{
  int data;
  struct node *nextpt;
} *headpt;

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 insertAtEnd(int num1) //the function which adds last node
{
  struct node *e;
  int p;
  p=num1;
  struct node *extra=(struct node*)malloc(sizeof(struct node));
  extra->data=p;
  e=headpt;
  while(e->nextpt!=headpt)
  {
   e=e->nextpt;
  }
  e->nextpt=extra;
  extra->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 circular list is :\n");
   }
  do
  {
   printf ("The data at node %d :: %d\n", n, extra->data);
   extra = extra->nextpt;
   n++;
  }
  while (extra != headpt);
 }
}

int main ()
{
  int n,x,num1;
  headpt = NULL;
  printf ("Enter the size of nodes: \n");
  scanf ("%d", &n);
  list (n);
  print (x);
  printf ("\nEnter the data to be inserted at the end: ");
  scanf ("%d", &num1);
  insertAtEnd(num1);
  print (x);
  return 0;
}
Output :-
Enter the size of nodes: 
5
Enter data for node 1 : 23
Enter data for node 1 : 10
Enter data for node 1 : 5
Enter data for node 1 : 7
Enter data for node 1 : 61
After insertion the circular list is :
The data at node 1 :: 23
The data at node 2 :: 10
The data at node 3 :: 5
The data at node 4 :: 7
The data at node 5 :: 61

Enter the data to be inserted at the end: 90
After insertion the circular list is :
The data at node 1 :: 23
The data at node 2 :: 10
The data at node 3 :: 5
The data at node 4 :: 7
The data at node 5 :: 61
The data at node 6 :: 90