#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
if (*head == NULL)
{
*head = newNode;
(*head)->next = *head;
size++;
return;
}
// if LL already as >=1 node
struct Node *curr = *head;
// traverse till last node in LL
while (curr->next != *head)
{
curr = curr->next;
}
curr->next = newNode;
newNode->next = *head;
*head = 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
if (*head == NULL)
{
// use insert function to insert
insert (head, data);
return;
}
// otherwise
struct Node *temp = *head;
// 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)
{
newNode->next = *head;
(*head)->next = newNode;
size++;
return;
}
// traverse to current mid position
while (--mid)
{
temp = temp->next;
}
newNode->next = temp->next;
temp->next = newNode;
size++;
}
void display (struct Node *head)
{
// if there are no node in LL
if (head == NULL)
return;
struct Node *temp = head;
//need to take care of circular structure of LL
do
{
printf ("%d ", temp->data);
temp = temp->next;
}
while (temp != head);
printf ("\n");
}
int main ()
{
struct Node *head = NULL;
insert (&head, 25);
insert (&head, 5);
display (head);
insertMiddle (&head, 10);
display (head);
insertMiddle (&head, 20);
display (head);
insertMiddle (&head, 15);
display (head);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *next;
};
int calcSize (struct Node *head);
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
if (*head == NULL)
{
*head = newNode;
(*head)->next = *head;
return;
}
// if LL already as >=1 node
struct Node *curr = *head;
// traverse till last node in LL
while (curr->next != *head)
{
curr = curr->next;
}
// assign LL's last node's next as this new node
curr->next = newNode;
// assign newNode's next as current head
newNode->next = *head;
// change head to this new node
*head = newNode;
}
void insertMiddle (struct Node **head, int data)
{
int size = calcSize (*head);
struct Node *newNode = (struct Node *) malloc (sizeof (struct Node));
newNode->data = data;
// if the LL was empty
if (*head == NULL)
{
// use insert function to insert
insertStart (head, data);
return;
}
// otherwise
struct Node *temp = *head;
// 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)
{
newNode->next = *head;
(*head)->next = newNode;
size++;
return;
}
// traverse to current mid position
while (--mid)
{
temp = temp->next;
}
newNode->next = temp->next;
temp->next = newNode;
size++;
}
int calcSize (struct Node *head)
{
int size = 0;
struct Node *temp = head;
if (temp == NULL)
return size;
do
{
size++;
temp = temp->next;
}
while (temp != head);
return size;
}
void display (struct Node *head)
{
// if there are no node in LL
if (head == NULL)
return;
struct Node *temp = head;
//need to take care of circular structure of LL
do
{
printf ("%d ", temp->data);
temp = temp->next;
}
while (temp != head);
printf ("\n");
}
int main ()
{
struct Node *head = NULL;
insertStart (&head, 25);
insertStart (&head, 5);
display (head);
insertMiddle (&head, 10);
display (head);
insertMiddle (&head, 20);
display (head);
insertMiddle (&head, 15);
display (head);
return 0;
}
Output
5 25
5 10 25
5 10 20 25
5 10 15 20 25
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Login/Signup to comment