I want to insert a node in middle , of a singly linked list
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our W3Make Forum to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
To insert a node in the middle of a singly linked list use the following code:
#include<stdio.h>
#include <stdlib.h>
struct node{
int info;
struct node* link;
};
struct node* start = NULL;
void traverse()
{
struct node* temp;
if(start == NULL)
printf(“LIST is empty”);
else{
temp = start;
while(temp!=NULL){
printf(“%d ->”,temp ->info);
temp=temp -> link;
}
printf(“\n”);
}
}
.
.
.
void insertinmiddle()
{
struct node*temp, *newnode;
int pos,data,i=1;
newnode=(struct node)malloc(sizeof(struct node));
printf(“Enter position and value: “);
scanf(“%d %d”,&pos, &data);
temp = start;
newnode->info=data;
newnode->link=0;
while(i<pos-1){
temp=temp->link;
i++;
}
newnode->link=temp->link;
temp->link=newnode;
}
.
.
.
return 0;
}