# C Program for Insertion at beginning in circular linked list ## Inserting a node at the starting of the list

In this section we will learn how to put new node at the starting of the list and the node which will be inserted in an empty list or an already existed list, so we have given a code here in C Program for insertion at the beginning in circular linked list.
In the circular linked list :-

• The last node of the list have a pointer to the first node of the list.
• The next pointer of the last node point to the head node of the list and the new node which is being inserted into the list will be the new head node of the list.

## Algorithm for inserting node at the starting of the circular linked list :-

• Step 1:   NOW IF POINTER = NULL (GO TO STEP 11)
• Step 2:   ASSIGN NEW NODE = POINTER
• Step 3: ASSIGN POINTER = POINTER → NEXT
• Step 4: ASSIGN NEW NODE → DATA = VALUE
• Step 5: ASSIGN TEMP NODE = HEAD
• Step 6: REPEAT STEP 8 WHILE TEMP → NEXT != HEAD (END OF WHILE LOOP)
• Step 7: ASSIGN TEMP NODE = TEMP → NEXT
• Step 8: ASSIGN NEW NODE → NEXT = HEAD
• Step 9: ASSIGN TEMP NODE → NEXT = NEW NODE
• Step 10: ASSIGN HEAD = NEW NODE
• Step 11: EXIT

## Code :-

```     struct node
{
int data;
struct node* next;
};``` ## Working require for putting node at the beginning of the circular linked list :-

• First 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.

## C Program for Insertion at beginning in doubly linked list :-

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

struct node
{
int data;
struct node *nextpt;

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;
{
printf ("The List is empty");
}
else
{
new_node = (struct node *) malloc (sizeof (struct node));
new_node->data = data;
{
cur_Node = cur_Node->nextpt;
}
cur_Node->nextpt = new_node;
}
}
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);
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;
}
}
}

void print (int m)          // print function of circular linked list
{
struct node *extra;
int n = 1;

{
printf ("The List is empty");
}
else
{
if (m == 1)
{
printf ("The circular linked list is :\n");
}
else
{
printf ("After insertion the list is :\n");
}
do
{
printf ("The data at node %d -> %d\n", n, extra->data);
extra = extra->nextpt;
n++;
}
}
}

int main ()
{
int n,value, x;
printf ("Enter the size of nodes: \n");
scanf ("%d", &n);
list (n);
print (x);
printf (" \nEnter the data to be inserted : ");
scanf ("%d", &value);
insertion_At_begin (value);
print (x);
return 0;
}```
```Output :-
Enter the size of nodes:
6
Enter data for node 1 : 7
Enter data for node 2 : 9
Enter data for node 3 : 10
Enter data for node 4 : 20
Enter data for node 5 : 30
Enter data for node 6 : 45
After insertion the list is :
The data at node 1 -> 7
The data at node 2 -> 9
The data at node 3 -> 10
The data at node 4 -> 20
The data at node 5 -> 30
The data at node 6 -> 45

Enter the data to be inserted : 55
After insertion the list is :
The data at node 1 -> 55
The data at node 2 -> 7
The data at node 3 -> 9
The data at node 4 -> 10
The data at node 5 -> 20
The data at node 6 -> 30
The data at node 7 -> 45
```