There are some errors, in your code.
I show you how to perform insertion 'at start' (that is replacing the
head
, if I got you)
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *ptr;
};
void display_linkedList(struct Node *head)
{
struct Node *temp = head;
while ((temp->ptr) != head)
{
printf("ELEMENT IS %d \n", temp->data);
temp = temp->ptr;
}
printf("ELEMENT IS %d \n", temp->data);
}
struct Node *insertion_at_the_start(struct Node * head, int value)
{
struct Node * new_node;
new_node = (struct Node *) malloc(sizeof(struct Node )); if ( ! new_node )
return new_node;
new_node->data = value;
new_node->ptr = head;
struct Node * next_node = head;
while ( next_node->ptr != head)
next_node = next_node->ptr;
next_node->ptr = new_node;
return new_node;
}
int main(int argc, char const *argv[])
{
struct Node * head;
struct Node *second;
head = (struct Node *)malloc(sizeof(struct Node));
second = (struct Node *)malloc(sizeof(struct Node));
head->data = 1;
head->ptr = second;
second->data = 2;
second->ptr = head;
printf("\nORIGINAL CIRCULAR LINKED LIST IS ----------------------------------\n");
display_linkedList(head);
printf("\nCIRCULAR LINKED LIST AFTER INSERTION AT START IS ----------------------------------\n");
head = insertion_at_the_start(head, 111111);
display_linkedList(head);
head = insertion_at_the_start(head, 222222);
display_linkedList(head);
return 0;
}