Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Insertion at the beginning in Circular Linked List in C++

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

How to insert new node at beginning in circular linked list in C++?

A circular linked list is a list of elements in which every element has a link to its next element in the list and the last element has a link to the first element. To perform insertion at the beginning in circular linked list in C++ we will traverse till the last node and we will insert new node after that node and will link the pointer of new node to the previous head. In this way our insertion at beginning in circular linked list is performed. In this article, we will learn more about the steps and algorithm to the same.

Steps for insertion at the beginning in Circular Linked List in CPP

These steps are followed while insertion of the data in the beginning of the circular linked list in C++

  1. Create a new node with data user want to insert.
  2. If head of the list is empty make new node as head
  3. If there is already some data in the head.
  4. Then define a node pointer(curNode) and make this head.
  5. Traverse this pointer until it reaches the last node of the list.
  6. Add new node here using the algorithm below
  7. Print new list using the display function to see the list with new data values.

How to create nodes of circular linked list?

Following set of code is used if we want to create nodes for circular linked list in our program.

class Node  

    int data; 
    Node *next; 
}; 
insertion at beginning in circular linked list in C++

Algorithm to write a CPP program to insert at beginning in Circular Linked List

This algorithm is used for insertion at beginning in circular linked list C++

  • STRUCT NEWNODE & CURNODE
  • IF HEAD==NULL
  • EXIT
  • ELSE
  • NEWNODE->NUM=NUM
  • NEWNODE->NEXT=HEAD
  • CURNODE=HEAD
  • WHILE(CURNODE->NEXT!=HEAD)
  • CURNODE=CURNODE->NEXT
  • EXIT WHILE
  • CURNODE->NEXT=NEWNODE
  • HEAD=NEWNODE

C++ program for insertion at beginning 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 insertBeginning(int num)//function to insert element at beginning
{
    struct node *newnode, *curNode;
    if(head == NULL)
    {
        cout<<"List is empty";
    }
    else
    {
        newnode = (struct node *)malloc(sizeof(struct node));
        newnode->num = num;
        newnode->next = head;
        curNode = head;
        while(curNode->next != head)
        {
            curNode = curNode->next;
        }
        curNode->next = newnode;
        head = newnode;
    }
}
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 beginning: ";
    cin>>num1;
    cout<<"\nAfter insertion data at beginning list is:";  
    insertBeginning(num1);
    display();	
    return 0;
}
Output:
Enter the size of circular linked list: 5
Enter data of the list:
12
13
14
15
16

Circular linked list data:
12 13 14 15 16 
Enter data to be inserted at beginning: 11

After insertion data at beginning list is:
Circular linked list data:
11 12 13 14 15 16

Want to learn more Data Structure?