-
Notifications
You must be signed in to change notification settings - Fork 2
/
6.vanilla_bs
338 lines (278 loc) · 5.37 KB
/
6.vanilla_bs
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
//only inserting at end function is reqd.
//OBSV CAREFULLY THE CHANGES BETWEEN Intersection and Union and Only Fn.
//1. In union:
// add A to C first and then check if element is there in B , dont insert, else insert.
// flag is reqd.
//2. In intersection
// check if common. if yes insert
//No use of flag
//3. ONly
//addfrom and checkin
// flag reqd.
//sapmle test case is univ:1 2 3 4 5 6
//van:1 2 3
//bs:2 4 1 6
#include <iostream>
using namespace std;
class node
{
string name;
int prn;
node* next;
public:
node()
{
name = "";
prn = 0;
}
friend class ll;
};
class ll
{
public:
node* head;
node* tail;
public:
ll()
{
head = tail = NULL;
}
void insert();//without node specification
void copy(node* nn);//with node specification
void display();
void uni(node* ahead, node* bhead);//union
void intersect(node* ahead, node* bhead);//intersect
void only(node* addfrom, node* checkin);// only van, only bs, neither-nor
};
void ll::only(node* ahead, node* bhead)
{
int flag = 0;
while (ahead != NULL)
{
flag = 0;
node* start = bhead;
while (start != NULL)
{
if (start->prn == ahead->prn)
{
flag++;
break;
}
start = start->next;
}
if (flag == 0)
{
node* nn = new node;
nn->name = ahead->name;
nn->prn = ahead->prn;
nn->next = NULL;
copy(nn);
}
ahead = ahead->next;
}
}
void ll::uni(node* ahead, node* bhead)
{
int flag = 0;
while (ahead != NULL)//copy everything in a in c
{
node* nn = new node;
nn->name = ahead->name;
nn->prn = ahead->prn;
nn->next = NULL;
copy(nn);
ahead = ahead->next;
}
while (bhead != NULL)//check if any same ele in b with c. if not ,insert in c.
{
node* chead = head;
flag = 0;
while (chead != NULL)
{
if (chead->prn == bhead->prn)
{
flag++;
break;
}
chead = chead->next;
}
if (flag == 0)
{
node* nn = new node;
nn->name = bhead->name;
nn->prn = bhead->prn;
nn->next = NULL;
copy(nn);
}
bhead = bhead->next;
}
}
void ll::intersect(node* addfrom, node*checkin )
{
while (addfrom != NULL)
{
node* start = checkin;
while (start != NULL)
{
if (addfrom->prn == start->prn)
{
node* nn = new node;
nn->name = start->name;
nn->prn = start->prn;
nn->next = NULL;
copy(nn);
break;
}
start = start->next;
}
addfrom = addfrom->next;
}
}
void ll::copy(node* nn)
{
if (head == NULL && tail == NULL)//empty
{
head = nn;
tail = nn;
}
else//not empty
{
tail->next = nn;
tail = nn;
}
}
void ll::insert()
{
node* nn = new node;
cout << "Enter name:" << endl;
cin >> nn->name;
cout << "Enter PRN:" << endl;
cin >> nn->prn;
nn->next = NULL;
if (head == NULL && tail == NULL)//empty
{
head = nn;
tail = nn;
cout << "---Inserted in empty list---" << endl;
}
else//not empty
{
tail->next = nn;
tail = nn;
cout << "---Insertion successfull---" << endl;
}
}
void ll::display()
{
node* start = head;
if (start == NULL)
{
cout << "---Nothing To Display---" << endl;
}
while (start != NULL)
{
cout << "Name:" << start->name << endl;
cout << "PRN:" << start->prn << endl;
start = start->next;
}
}
int main()
{
int ch,ch2,ch3,ch4;
cout << "Univ Set:Set of All Students" << endl;
ll univ;
do
{
cout << "Insert element" << endl;
univ.insert();
cout << "Enter 0 to stop" << endl;
cin >> ch4;
} while (ch4 != 0);
cout << "\n\tUniversal Set" << endl;
univ.display();
cout << "Set A: Vanilla" << endl;
ll a;
do
{
cout << "Insert element" << endl;
a.insert();
cout << "Enter 0 to stop" << endl;
cin >> ch;
} while (ch != 0);
cout << "\n\tSet A" << endl;
a.display();
cout << "Set B :Butterscotch" << endl;
ll b;
do
{
cout << "Insert element" << endl;
b.insert();
cout << "Enter 0 to stop" << endl;
cin >> ch2;
} while (ch2 != 0);
cout << "\n\tSet B" << endl;
b.display();
do
{
cout << "\n\tEnter your choice" << endl;
cout << "\n\t1.Set of students who like either vanilla or butterscotch or both " << endl;
cout << "\n\t2. Set of students who like both vanilla and butterscotch " << endl;
cout << "\n\t3.Set of students who like only vanilla not butterscotch " << endl;
cout << "\n\t4. Set of students who like only butterscotch not vanilla " << endl;
cout << "\n\t5.Number of students who like neither vanilla nor butterscotch. " << endl;
cout << "\n\t6.Exit" << endl;
cin >> ch3;
switch(ch3)
{
case 1:
{
cout << "\n\tUNION" << endl;
ll c;
c.uni(a.head, b.head);
c.display();
break;
}
case 2:
{
cout << "\n\tIntersection" << endl;
ll d;
d.intersect(a.head, b.head);
d.display();
break;
}
case 3:
{
cout << "\n\tOnly Vanilla" << endl;
ll e;
e.only(a.head, b.head);//the one in which you wanna subtract stuff.it is first parameter
e.display();
break;
}
case 4:
{
cout << "\n\tOnly Butterscotch" << endl;
ll f;
f.only(b.head, a.head);//the one in which you wanna subtract stuff.it is first parameter
f.display();
break;
}
case 5:
{
cout << "\n\tNeither Vanilla nor Butterscotch" << endl;
ll g;
ll uni;
uni.uni(a.head, b.head);
g.only(univ.head, uni.head);//subtract from univ the ele in union set.
g.display();
break;
}
case 6:
{
cout << "Thank You!!!" << endl;
break;
}
default:cout << "Invalid Choice" << endl;
}
} while (ch3 != 6);
return 0;
}