Insertion at end in Circular Linked List in C++

C++ program for insertion at end in circular linked list

How to insert at end in circular linked list in CPP?

In single linked list, every node points to its next node in the list and the last node points NULL. But in circular linked list, every node points to its next node in the list but the tail node points to the head node in the list.In this article, we will learn how code a program for insertion at end in circular linked list in C++ programming language.

We can also perform insertion at end in doubly linked list. If you want to learn the same click the button below

Steps to write a C++ program for insertion at the end in circular linked list

Following steps are followed if we want to insert data at the end of the circular linked list in CPP

  1. Create new node with the help of data given.
  2. If the circular linked list is empty make new node as head of the list.
  3. And if the list has already some data present in it then,struct a node pointer say p, and initialize with head.
  4. Traverse until the next of the pointer points the head.
  5. Set p → next = temp and temp → next = head, this will link newly inserted node with the previous head.
  6. Print the new list with the help of print function.

How to build node of a circular linked list?

Syntax

class Node  

    int data; 
    Node *next; 
}; 
Insertion at end in circular linked list in C++

Algorithm to write a C++ program for insertion at the end in circular linked list

Following algorithm is followed for insertion at end in circular linked list C++

  • STRUCT NODE P
  • INTEGER A=NUM1
  • STRUCT NODE TEMP
  • TEMP->NUM=A;
  • P=HEAD
  • WHILE(P->NEXT!-HEAD)
  • P=P->NEXT
  • END WHILE
  • P->NEXT=TEMP
  • TEMP->NEXT=HEAD

C++ program for insertion at end in circular linked list

#include <iostream>
using namespace std;

struct node {
    int num;
    struct node * next;
}*head;

void make(int n)//function to build linked list
{
    int i, num;
    struct node *preptr, *newnode;

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

        cout<<"Enter data of the list:\n"; cin>>num;
        head->num = num;
        head->next = NULL;
        preptr = head;
        for(i=2; i<=n; i++) { newnode = (struct node *)malloc(sizeof(struct node)); cin>>num;
            newnode->num = num;
            newnode->next = NULL;	
            preptr->next = newnode;	
            preptr = newnode;   		
        }
        preptr->next = head; 		//linking last node with head node
    }
}

void insertEnd(int num1)//function to insert element at end 
{
        struct node *p;
		int a;
		a=num1;
		struct node *temp=(struct node*)malloc(sizeof(struct node));
		temp->num=a;
		p=head;
		while(p->next!=head)
		{
			p=p->next;
		}
		p->next=temp;
		temp->next=head;
}
void display()//function to display list
{
    struct node *tmp;
    int n = 1;
    
    if(head == NULL)
    {
        cout<<"List is empty";
    }
    else
    {
        tmp = head;
        
        cout<<"\nCircular linked list data:\n";
        do {
            cout<num<<" "; tmp = tmp->next;
            n++;
        }while(tmp != head);
    }
}
int main()//main function
{
    int n,num1;
    head = NULL;
    cout<<"Enter the size of circular linked list: ";
    cin>>n;
    make(n); 
    display();
	cout<<"\nEnter data to be inserted at end: ";
    cin>>num1;
    cout<<"\nAfter insertion data at end list is:";  
    insertEnd(num1);
    display();	
    return 0;
}
Output:
Enter the size of circular linked list: 5
Enter data of the list:
11
21
31
41
51

Circular linked list data:
11 21 31 41 51 
Enter data to be inserted at end: 61

After insertion data at end list is:
Circular linked list data:
11 21 31 41 51 61

   Learn to delete from end in