insertion at beginning in circular linked list
Data Structure

Insertion at Beginning in Circular Singly Linked List

Insertion at Beginning in Circular Singly Linked List

  • In Previous Tutorial we have already discussed and explained about how to create and display a Circular Singly Linked List in Data Structure in C.
  • In this tutorial we will learn how to insert a new node at the beginning of the Circular Singly Linked List.
  • To insert a new node at the beginning in the circular linked list we have to follow the steps given here.

First we define the structure of node

Structure of Node

struct node

{

int data ;

struct node * next;

};

Also create a head pointer

struct node *head ;

At first we create a circular linked with n nodes using the approach discussed in the tutorial Circular Linked List in Data Structure and after that we perform insertion in the beginning of circular linked list.

Algorithm to perform insertion at beginning in circular linked list is given below –

Step1 – Create a New Node.

struct node * newnode ;

Step 2 – Allocate memory to newly Created Node.

Step 3 – Assign data field value to newnode.

Step 4. To add new node in the beginning set the next pointer of newnode as

head—>next and next pointer of lastnode  with new node.

Step 5 – Set head=newnode.

Pseudo Code or function to Insert newnode at the beginning of circular linked list is given below

void insertAtBeginning(int value)

{

       struct node * newNode ;

       newNode = (struct node *)malloc(sizeof(struct node));

        newNode->data = value;

        newNode->next = head; // Point to next node which is currently head

        lastnode->next =newNode; //Connect last node with newNode which is now Firstnode

        head=newNode

}

C Program to insert new node in the beginning of Circular Singly Linked List is given below 

#include <stdio.h>
#include <stdlib.h>

// First we define the structure of node

struct node
{
int data;
struct node * next;
};
struct node *head,*firstnode,*lastnode;

// Declare function to create , display and insert the node in linked list

void createList(int n);
void displayList();
void insertAtBeginning(int value);

int main()
{
int numnodes, value1;
printf(“enter the number of nodes”);
scanf(“%d”, &numnodes);
createList(numnodes);
displayList();
printf(“enter the data field value of new node to be insert”);
scanf(“%d”,&value1);
insertAtBeginning(value1);
printf(“linkedlist after insert newnode at beginning is”);
displayList();
return 0;
}

/* * Creates a circular linked list of n nodes using function */

void createList(int n)
{
int i, data;
struct node *newNode;
firstnode = (struct node *)malloc(sizeof(struct node));
head=firstnode;
printf(“Enter data of 1 node: “);
scanf(“%d”, &data);
firstnode->data = data;
firstnode->next = NULL;
lastnode = firstnode ;
for(i=2; i<=n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node)) ;
printf(“Enter data of %d node: “, i);
scanf(“%d”, &data);
newNode->data = data;
newNode->next = NULL;
// Link the previous node with newly created node
lastnode->next = newNode;
// Move the previous node ahead
lastnode = newNode;
}
// when Linked List is created with n nodes then to make it circular Link // the last node with first node
lastnode->next=head;
printf(“\nCIRCULAR LINKED LIST CREATED SUCCESSFULLY\n”);
}

/** Display the content of the list using function */

void displayList()
{
struct node *temp;
if(head == NULL)
{
printf(“List is empty.\n”);
}
else
{
temp = head;
printf(“Linked List is\n”);
do {
printf(“%d\t”, temp ->data);
temp = temp->next;
}while(temp!= head);
}
}

/*Pseudocode or Function to insert new node at the beginning of circular*/

linkedlist is given below*/
void insertAtBeginning(int value)
{
struct node * newNode ;
if(head == NULL)
{
printf(“Error, List is Empty!\n”);
}
else
{
newNode = (struct node *)malloc(sizeof(struct node));
newNode->data = value;
newNode->next = head; // Point to next node which is currently head
lastnode->next =newNode; //Connect last node with newNode which is now Firstnode
head=newNode ;
}
}

OUTPUT

enter the number of nodes
4

Enter data of 1 node: 21

Enter data of 2 node: 22

Enter data of 3 node: 23

Enter data of 4 node: 24

CIRCULAR LINKED LIST CREATED SUCCESSFULLY

Linked List is

21 22 23 24 enter the data field value of new node to be insert 45

linkedlist after insert newnode at beginning

Linked List is

45 21 22 23 24

Conclusion and Summary

  • In this tutorial we have explained the algorithm  for insertion at beginning in circular linked list.
  • C Program to insert a node in the beginning of circular linked list is also explained.

 

Next Tutorial – Insertion at the End of Circular Linked List

Leave a Reply

Your email address will not be published. Required fields are marked *