# C Program for Circular Linked List Insertion in the middle

## Circular Linked List insertion in the middle in C

We will write a Program to do insertion in the middle in a Circular Linked List in C We will be looking at two different approaches

• Approach 1: Follows solution with an extra size variable that keeps track of the current size of Circular Linked List
• Approach 2: Follows solution where we have an additional function to calculate the size of the circular linked list anytime.

### Structure Of Node

```     struct node
{
int data;
struct node* next;
};``` Run
```#include<stdio.h>
#include<stdlib.h>

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

int size = 0;

void insert (struct Node **head, int data)
{

struct Node *newNode = (struct Node *) malloc (sizeof (struct Node));
newNode->data = data;

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

// if LL already as >=1 node

// traverse till last node in LL
{
curr = curr->next;
}
curr->next = newNode;

size++;
}

void insertMiddle (struct Node **head, int data)
{

struct Node *newNode = (struct Node *) malloc (sizeof (struct Node));
newNode->data = data;

// if the LL was empty
{
// use insert function to insert
return;
}

// otherwise

// find correct insertion position for middle
int mid = (size % 2 == 0) ? (size / 2) : (size + 1) / 2;

// Unique case when there is only one node in CLL
// we will be inserting at the last,
// inserting 2nd node at the last
// Example size = 1 will result in mid = 1 so entering after 1st node
// where size itself is 1 so entering last node
if (mid == size)
{
size++;
return;
}
// traverse to current mid position
while (--mid)
{
temp = temp->next;
}

newNode->next = temp->next;
temp->next = newNode;
size++;
}

{
// 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

```5 25
5 10 25
5 10 20 25
5 10 15 20 25```
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
}

void insertMiddle (struct Node **head, int data)
{

struct Node *newNode = (struct Node *) malloc (sizeof (struct Node));
newNode->data = data;

// if the LL was empty
{
// use insert function to insert
return;
}

// otherwise

// find correct insertion position for middle
int mid = (size % 2 == 0) ? (size / 2) : (size + 1) / 2;

// Unique case when there is only one node in CLL
// we will be inserting at the last,
// inserting 2nd node at the last
// Example size = 1 will result in mid = 1 so entering after 1st node
// where size itself is 1 so entering last node
if (mid == size)
{
size++;
return;
}
// traverse to current mid position
while (--mid)
{
temp = temp->next;
}

newNode->next = temp->next;
temp->next = newNode;
size++;
}

{
int size = 0;

if (temp == NULL)
return size;

do
{
size++;
temp = temp->next;

}

return size;
}

{
// 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;
}
```

```5 25
5 10 25
5 10 20 25
5 10 15 20 25
```

### 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