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

• 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