-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConcatenateList.c
203 lines (194 loc) · 4.76 KB
/
ConcatenateList.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Concatenate Linked List
#include<stdio.h>
#include<stdlib.h>
struct node {
int info;
struct node *next;
};
struct node* getnode()
{
return (struct node*)malloc(sizeof(struct node));
}
struct node *list1 = NULL;
struct node *list2 = NULL;
struct node *list3 = NULL;
void insertbeg(struct node *list, int x)
{
if(list == NULL)
{
struct node *nn;
nn = getnode();
nn->info = x;
nn->next = NULL;
list = nn;
}
else
{
struct node *nn;
nn = getnode();
nn->info = x;
nn->next = list;
list = nn;
}
}
void insertend(struct node *list, int x)
{
if(list == NULL)
{
int ch;
printf("List is Empty!\n");
printf("Do you want to insert at beginning?\n");
printf("Enter 1 to do so : ");
scanf("%d", &ch);
if(ch == 1)
{
insertbeg(list, x);
}
}
else
{
struct node *nn, *temp;
temp = list;
nn = getnode();
nn->info = x;
nn->next = NULL;
while(temp->next!=NULL)
{
temp = temp->next;
}
temp->next = nn;
}
}
void show(struct node* list)
{
struct node *temp;
temp = list;
while(temp!=NULL)
{
printf("%d ", temp->info);
temp = temp->next;
}
printf("\n");
return;
}
struct node* concat(struct node *list1, struct node *list2)
{
struct node *list3 = NULL;
struct node *temp;
temp = list1;
while(temp->next!=NULL)
{
insertend(list3, temp->info);
temp = temp->next;
}
temp = list3;
while(temp->next!=NULL)
{
temp = temp->next;
}
temp->next = list2;
temp = list2;
while(temp->next!=NULL)
{
insertend(list3, temp->info);
temp = temp->next;
}
return list3;
}
int main()
{
int ch, n;
while(1)
{
printf("-----main options----\n");
printf("1. Perform ops on List1\n");
printf("2. Perform ops on List2\n");
printf("3. Concatenate List1 and List2\n");
printf("4. Exit");
printf("Enter your choice : ");
scanf("%d", &ch);
if(ch == 1)
{
while(1)
{
printf("----list1 menu----\n");
printf("1. Insert at beginning\n");
printf("2. Insert at end\n");
printf("3. Show\n");
printf("4. Exit\n");
printf("Enter your choice : ");
scanf("%d", &ch);
switch(ch)
{
case 1:
printf("Enter a value to insert at beg : ");
scanf("%d", &n);
insertbeg(list1, n);
break;
case 2:
printf("Enter a value to insert at end : ");
scanf("%d", &n);
insertend(list1, n);
break;
case 3:
printf("List 1 : ");
show(list1);
break;
case 4:
exit(0);
break;
default:
printf("Invalid Choice! Try Again!\n");
}
}
}
else if(ch == 2)
{
while(1)
{
printf("----list2 menu----\n");
printf("1. Insert at beginning\n");
printf("2. Insert at end\n");
printf("3. Show\n");
printf("4. Exit\n");
printf("Enter your choice : ");
scanf("%d", &ch);
switch(ch)
{
case 1:
printf("Enter a value to insert at beg : ");
scanf("%d", &n);
insertbeg(list2, n);
break;
case 2:
printf("Enter a value to insert at end : ");
scanf("%d", &n);
insertend(list2, n);
break;
case 3:
printf("List 2 : ");
show(list2);
break;
case 4:
exit(0);
break;
default:
printf("Invalid Choice! Try Again!\n");
}
}
}
else if(ch == 3)
{
list3 = concat(list1, list2);
show(list3);
}
else if(ch == 4)
{
exit(0);
}
else
{
printf("Invalid Choice Try Again!\n");
}
}
}