-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathlinkedlist_concatenation.c
151 lines (89 loc) · 1.71 KB
/
linkedlist_concatenation.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct Node
{
int rollno;
struct Node *Next;
};
struct Node *Create_List();
void show(struct Node *);
struct Node *Concat(struct Node *, struct Node *);
int main(int argc, char const *argv[])
{
struct Node *h1, *h2, *p;
printf("enter the number of Node to be inserted in list 1\n");
h1 = Create_List();
printf("enter the number of Node to be inserted in list 2\n");
h2 = Create_List();
printf("showing a list1\n");
show(h1);
printf("showing a list 2\n");
show(h2);
p = Concat(h1, h2);
printf("list after concatenating\n");
show(p);
return 0;
}
struct Node *
Create_List()
{
int k, n;
scanf("%d", &n);
struct Node *p, *h;
for (k = 0; k < n; k++)
{
if (k == 0)
{
h = (struct Node *)malloc(sizeof(struct Node));
p = h;
}
else
{
p->Next = (struct Node *)malloc(sizeof(struct Node));
p = p->Next;
}
scanf("%d", &p->rollno);
}
p->Next = NULL;
return h;
}
struct Node *
Concat(struct Node *h1, struct Node *h2)
{
struct Node *p;
if (h1 == NULL)
{
h1 = h2;
return h1;
}
if (h2 == NULL)
{
return h1;
}
p = h1;
while (p->Next != NULL)
{
p = p->Next;
}
p->Next = h2;
return h1;
}
void show(struct Node *h)
{
struct Node *p;
if (h == NULL)
{
printf("list is empty\n");
}
else
{
p = h;
while (p != NULL)
{
printf("%d ", p->rollno);
p = p->Next;
}
printf("\n");
}
}