C Program
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
struct node *start=NULL;
void display(){
struct node *p=start;
printf("Linked List:");
while(p){
printf("->%d",p->data);
p=p->next;
}
}
int main(){
int n,i,x,pv,ins;
struct node *p1,*p2,*t;
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d",&x);
p1=(struct node*)malloc(sizeof(struct node));
p1->data=x; p1->next=NULL;
if(start==NULL) start=t=p1;
else{ t->next=p1; t=p1; }
}
scanf("%d%d",&pv,&ins);
for(p1=start;p1;p1=p1->next)
if(p1->data==pv){
p2=(struct node*)malloc(sizeof(struct node));
p2->data=ins;
p2->next=p1; // mandatory keyword
p2->next=p1->next;
p1->next=p2;
break;
}
if(!p1) printf("Node not found! ");
display();
}
0 Comments