forked from XdithyX/Ds-lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedlist2.c
80 lines (66 loc) · 1.09 KB
/
linkedlist2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*first=NULL;
void create(int a[],int n)
{
int i;
struct node *t,*last;
first=(struct node *)malloc(sizeof(struct node));
first->data=a[0];
first->next=NULL;
last=first;
for(i=1;i<n;i++)
{
t= (struct node *)malloc(sizeof(struct node));
t->data=a[i];
t->next=NULL;
last->next=t;
last=t;
}
}
void addele()
{
printf("Enter the node data after which new node is to be inserted and the node data to be inserted\n");
int x,y;
scanf("%d %d",&x,&y);
struct node *newnode;
newnode=(struct node *)malloc(sizeof(struct node));
struct node *t=first;
do
{
if(t->data==x)
{
newnode->data=y;
newnode->next=t->next;
t->next=newnode;
break;
}
else
t=t->next;
}while(t->next!=NULL);
}
void display(struct node *p)
{
while(p!=NULL)
{
printf("%d\n",p->data);
p=p->next;
}
}
void main()
{
int n,a[20],i,c;
printf("Enter the number of elements");
scanf("%d",&n);
printf("Enter the elements");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
create(a,n);
display(first);
addele();
display(first);
}