Click here to Skip to main content
15,899,124 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#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); 
free(temp); 
} 
struct Node *insertion_At_the_start(struct Node *head, int value) 
{ 
struct Node *n; 
n = (struct Node *)malloc(sizeof(struct Node *)); //creation of new node 
n->data = value; 
 
 
struct Node *temp = head; 
 
while (temp->ptr != head) 
{ 
 
temp = temp->ptr; 
} 
 
 
 
temp->ptr = n; 
n->ptr = head; 
 
return n; 
} 
struct Node *insertion_At_the_end(struct Node *head, int value) 
{ 
struct Node *n; 
n = (struct Node *)malloc(sizeof(struct Node *)); //creation of new node 
n->data = value; 
n->ptr = head; 
 
 
struct Node *temp = head; 
 
while ((temp->ptr) != head) 
{ 
printf("!"); 
temp = temp->ptr; 
} 
 
n->ptr = temp->ptr; 
 
 
return head; 
} 
int main(int argc, char const *argv[]) 
{ 
struct Node *head; 
struct Node *second; 
struct Node *third, *fourth; 
 
head = (struct Node *)malloc(sizeof(struct Node)); 
second = (struct Node *)malloc(sizeof(struct Node)); 
 
third = (struct Node *)malloc(sizeof(struct Node)); 
 
fourth = (struct Node *)malloc(sizeof(struct Node)); 
 
head->data = 1; 
head->ptr = second; 
 
second->data = 2; 
second->ptr = third; 
 
third->data = 3; 
third->ptr = fourth; 
 
fourth->data = 4; 
fourth->ptr = head; 
 
printf("\nORIGINAL CIRCULAR LINKED LIST IS ----------------------------------\n"); 
display_linkedList(head); 
 
printf("\nCIRCULAR LINKED LIST AFTER INSERTION AT START IS ----------------------------------\n"); 
 
display_linkedList(insertion_At_the_start(head, 111111)); 
 
display_linkedList(insertion_At_the_end(head, 5555)); 
return 0; 
} 


What I have tried:

i have tired all sorts of things but this issue is not getting resolved
Posted
Updated 24-Aug-21 7:03am

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)

C
#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);
  // free(temp); ERROR!
}

struct Node *insertion_at_the_start(struct Node * head, int value)
{
  struct Node * new_node;
  new_node = (struct Node *) malloc(sizeof(struct Node )); //creation of new node 
  if ( ! new_node )
    return new_node; // handle possible malloc failure

  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;
}
 
Share this answer
 
There is a magic tool for helping you out: the debugger. Start learning Using the debugger tutorial and find your mistake.

Start with some polishing of your code by using indention and some output in critical code lines.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900