C Program for Insertion in circular linked list

Learn how to insert node in the circular linked list

In the following section we will learn how to write a code in C Program for insertion in circular linked list, insertion means add a new node in the empty or already existed linked list.
We can perform insertion at different positions such as:-

  • Insertion at the beginning
  • Insertion at the end
  • Insertion at the nth node

 

Intro of Types of insertion in circular linked list
Types of insertion in circular linked list in C

Types of insertion in circular linked list :-

1. Insertion at beginning:-

Insertion at starting in circular linked list in which we have to insert a new node before the head of the linked list, then the new node will become the head of the linked list.

2. Insertion at end:-

Insertion at last in circular linked list in which we have to add a new node at the end of the list, then the new node will become the tail of the linked list.

3. Insertion at nth node :-

Insertion at specific position means a new node is added according to the user wish in the linked list.

B

E

G

I

N

N

I

N

G

Insertion at beginning in linked list the steps are followed :-

  1. Make the linked list.
  2. Then we have to take an extra pointer which points to the end node of the circular linked list.
  3. Then we have a pointer that is pointing to the end node, then end node-> next will point to the first node.
  4. At last follow the algorithm for insertion at beginning in circular linked list given below.

B

E

G

I

N

N

I

N

G

Insertion at beginning in linked list the steps are followed :-

  • Make the linked list.
  • Then we have to take an extra pointer which points to the end node of the circular linked list.
  • Then we have a pointer that is pointing to the end node, then end node-> next will point to the first node.
  • At last follow the algorithm for insertion at beginning in circular linked list given below.

Code snippet for insertion at the beginning :-

void insertion_At_begin (int data)      // insertion function used for node to put in beginning of circular linked list
{
  struct node *new_node, *cur_Node;
  if (headpt == NULL)
    {
      printf ("The List is empty");
    }
  else
    {
      new_node = (struct node *) malloc (sizeof (struct node));
      new_node->data = data;
      new_node->nextpt = headpt;
      cur_Node = headpt;
      while (cur_Node->nextpt != headpt)
	{
	  cur_Node = cur_Node->nextpt;
	}
      cur_Node->nextpt = new_node;
      headpt = new_node;
    }
}
C Program for Insertion at the beginning in the circular Linked List

E

N

D

Insertion at end in linked list the steps are followed :-

  1. Make a new node.
  2. Assign the new node next to circular list.
  3. If the list is empty then return new node.
  4. Assign the new node next to the front of the list.
  5. Assign tail next to the new node.
  6. Return the end node of the circular linked list.

E

N

D

Insertion at end in linked list the steps are followed :-

  1. Make a new node.
  2. Assign the new node next to circular list.
  3. If the list is empty then return new node.
  4. Assign the new node next to the front of the list.
  5. Assign tail next to the new node.
  6. Return the end node of the circular linked list.

Code snippet for insertion at the end:-

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;
}
C Program for Insertion at the end in circular linked list

  Nth

N

O

D

E

Insertion in between the nodes in linked list the steps are followed :-

  1. Make a new node and set the data.
  2. Move to pos-1 position in the circular linked list.
  3. Now link the next pointer of new node with the node pointed by the next pointer of current(pos-1) node.
  4. After that join the next pointer of current node with the newly created node which means that the next pointer of current node will point to new node.
  5. Now print the linked list.
  6. Learn algorithm given below to understand better.

  Nth

N

O

D

E

Insertion in between the nodes in linked list the steps are followed :-

  1. Make a new node and set the data.
  2. Move to pos-1 position in the circular linked list.
  3. Now link the next pointer of new node with the node pointed by the next pointer of current(pos-1) node.
  4. After that join the next pointer of current node with the newly created node which means that the next pointer of current node will point to new node.
  5. Now print the linked list.
  6. Learn algorithm given below to understand better.

Code snippet for insertion at the specific position:-

void insert_At_Any_Node (int data, int pos) //function for insertion at user required position
{
  struct node *new_node, *cur_Node;
  int i;

  if (headpt == NULL)
    {
      printf ("List is empty");
    }
  else if (pos == 1)
    {
      insertion_At_begin (data);
    }
  else
    {
      new_node = (struct node *) malloc (sizeof (struct node));
      new_node->data = data;
      cur_Node = headpt;
      for (i = 2; i nextpt;
	}
      new_node->nextpt = cur_Node->nextpt;
      cur_Node->nextpt = new_node;
    }
}
C Program for Insertion at nth node in circular linked list