# 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

### Structure of node in Linked List

```struct node
{
int data;
struct node* next;
};```

### Working Required

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

### Code For Insertion At The Beginning In Circular Linked List

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

struct Node
{
int data;
struct Node *next;
};

void insertStart (struct Node **head, int data)
{
struct Node *newNode = (struct Node *) malloc (sizeof (struct Node));
newNode->data = data;

// if its the first node being entered
{
return;
}

// if LL already as >=1 node

// traverse till last node in LL
{
curr = curr->next;
}
// assign LL's last node's next as this new node
curr->next = newNode;

// assign newNode's next as current head

// change head to this new node
}

{
// if there are no node in LL
return;

//need to take care of circular structure of LL
do
{
printf ("%d ", temp->data);
temp = temp->next;

}
printf ("\n");
}

int main ()
{

return 0;
}
```

### Output

```Linked List: 1 2 3 4 5 6
```

### Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

## Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

## Checkout list of all the video courses in PrepInsta Prime Subscription

• Introduction to Circular Linked List
• Circular Linked List in –
C | C++ | Java
• Insertion in Circular Linked List –
C | C++ | Java
• Insertion at the beginning–
C | C++ | Java
• Insertion at the end –
C | C++ | Java
• Insertion at nth position –
C | C++ | Java
• Deletion in Circular Linked List –
C | C++ | Java
• Deletion from beginning in Circular Linked List –
C | C++ | Java
• Deletion from nth position in Circular Linked List –
• Deletion from end in Circular Linked List –
C | C++ | Java
• Insertion and Deletion in Circular Linked List – C | C++ | Java
• Split a Circular Linked List in two halves –
C | C++ | Java
• Count nodes in Circular Linked List –
C | C++ | Java
• Sorted Insert In Circular Linked List –
C | C++ | Java
• Insertion in the middle in Circular Linked List –
C | C++ | Java