-
Notifications
You must be signed in to change notification settings - Fork 0
/
Practical_19(C++).cpp
347 lines (320 loc) · 7.95 KB
/
Practical_19(C++).cpp
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
339
340
341
342
343
344
345
346
347
/*
Department of Computer Engineering has student's club named
'Pinnacle Club'. Students of Second, third and final year of
department can be granted membership on request. Similarly one
may cancel the membership of club. First node is reserved for
president of club and last node is reserved for secretary of
club. Write C++ program to maintain club member‘s information
using singly linked list. Store student PRN and Name. Write
functions to
a) Add and delete the members as well as president or even
secretary.
b) Compute total number of members of club
c) Display members
d) Display list in reverse order using recursion
e) Two linked lists exists for two divisions. Concatenate two
lists
*/
#include <iostream>
#include <string>
using namespace std;
class list;
class node {
int prn;
string name;
node *next;
public:
node(int x, string nm) {
prn = x;
next = NULL;
name = nm;
}
friend class list;
};
class list {
node *start;
public:
list() {
start = NULL;
}
void create();
void display();
void insertAtBeginning();
void insertAtEnd();
void insertAfter();
void deleteAtFirst();
void deleteByValue();
void deleteAtEnd();
int computeTotal();
void sortList();
void concatList(list &q1);
void displayRev(node *t);
bool reverseDisplay();
};
void list::displayRev(node *t) {
if (t == NULL)
return;
else {
displayRev(t->next);
cout << "\nPRN NO:" << t->prn << " Name: " << t->name;
}
}
void list::create() {
int no;
string nam;
if (start == NULL) {
cout << "Enter PRN number: ";
cin >> no;
cout << "Enter name: ";
cin >> nam;
cout << nam;
start = new node(no, nam);
cout << "\n=============== List Created ===============";
} else {
cout << "\nList is already created.";
}
}
void list::display() {
node *t;
t = start;
if (start == NULL)
cout << "\nList is Empty";
else {
cout << "\n=============== List: ===============\n";
while (t != NULL) {
cout << t->prn << " " << t->name << " \n";
t = t->next;
}
}
}
void list::insertAtBeginning() {
int no;
string nam;
node *temp;
if (start == NULL) {
create();
} else {
cout << "\nEnter PRN Number: ";
cin >> no;
cout << "Enter Name: ";
cin >> nam;
temp = new node(no, nam);
temp->next = start;
start = temp;
cout << "Inserted " << temp->name << " at the beginning.";
}
}
void list::insertAtEnd() {
int no;
string nam;
node *t;
if (start == NULL)
create();
else {
cout << "\nEnter PRN Number: ";
cin >> no;
cout << "Enter Name: ";
cin >> nam;
t = start;
while (t->next != NULL)
t = t->next;
node *p = new node(no, nam);
t->next = p;
}
}
void list::insertAfter() {
int prev_no;
cout << "\nEnter PRN No. after which you want to insert: ";
cin >> prev_no;
node *t;
t = start;
string nam;
int flag = 0, no;
while (t != NULL) {
if (t->prn == prev_no) {
flag = 1;
break;
}
t = t->next;
}
if (flag == 1) {
node *p;
cout << "\nEnter PRN Number: ";
cin >> no;
cout << "Enter Name: ";
cin >> nam;
p = new node(no, nam);
p->next = t->next;
t->next = p;
} else {
cout << "\n" << prev_no << " is not in the list.";
}
}
void list::deleteAtFirst() {
node *t;
if (start == NULL)
cout << "\nClub is Empty..";
else {
t = start;
start = start->next;
t->next = NULL;
delete t;
cout << "\nPresident deleted..";
}
}
void list::deleteByValue() {
int no, flag = 0;
node *t, *prev;
if (start == NULL)
cout << "\nList/Club is empty";
else {
cout << "\nEnter PRN No. of member to be deleted: ";
cin >> no;
t = start->next;
while (t->next != NULL) {
if (t->prn == no) {
flag = 1;
break;
}
prev = t;
t = t->next;
}
if (flag == 1) {
prev->next = t->next;
t->next = NULL;
delete t;
cout << "\nMember with PRN No: " << no << " is deleted.";
} else
cout << "\nMember not found in List./President or Secretary cannot be deleted.";
}
}
void list::deleteAtEnd() {
node *t, *prev;
t = start;
if (start == NULL)
cout << "\nClub is Empty..";
else {
while (t->next != NULL) {
prev = t;
t = t->next;
}
prev->next = NULL;
delete t;
cout << "\nSecretary Deleted.";
}
}
int list::computeTotal() {
node *t;
int count = 0;
t = start;
if (start == NULL) {
cout << "\nList is empty.";
return 0;
}
while (t != NULL) {
count++;
t = t->next;
}
return count;
}
void list::sortList() {
node *i, *j, *last = NULL;
int tprn;
string tname;
if (start == NULL) {
cout << "\nList is empty.";
return;
}
for (i = start; i->next != NULL; i = i->next) {
for (j = start; j->next != last; j = j->next) {
if ((j->prn) > (j->next->prn)) {
tprn = j->prn;
tname = j->name;
j->prn = j->next->prn;
j->name = j->next->name;
j->next->prn = tprn;
j->next->name = tname;
}
}
}
cout << "\nList is sorted.";
display();
}
void list::concatList(list &q1) {
node *t, *p;
t = q1.start;
if (t == NULL) {
cout << "\nList 2 is empty";
return;
}
p = start;
while (p->next != NULL) {
p = p->next;
}
p->next = t;
q1.start = NULL;
cout << "\nAfter concatenation list:\n";
display();
}
bool list::reverseDisplay() {
if (start == NULL)
return false;
node *temp = start;
displayRev(temp);
return true;
}
int main() {
list *l;
int choice, selectList;
list l1, l2;
l = &l1;
X:
cout << "\nSelect List\n1.List 1\n2.List 2\nEnter choice: ";
cin >> selectList;
if (selectList == 1) {
l = &l1;
} else if (selectList == 2) {
l = &l2;
} else {
cout << "\nWrong list Number.";
goto X;
}
do {
cout << "\n1. Create\n2. Insert President\n3. Insert secretary\n4. Insert after position(member)\n";
cout << "5. Display list\n6. Delete President\n7. Delete Secretary\n8. Delete Member\n";
cout << "9. Find total No. of members\n10. Sort list\n11. Reselect List\n";
cout << "12. Combine lists\n13. Reverse Display\n0. Exit\nEnter your choice: ";
cin >> choice;
switch (choice) {
case 1: l->create();
break;
case 2: l->insertAtBeginning();
break;
case 3: l->insertAtEnd();
break;
case 4: l->insertAfter();
break;
case 5: l->display();
break;
case 6: l->deleteAtFirst();
break;
case 7: l->deleteAtEnd();
break;
case 8: l->deleteByValue();
break;
case 9: cout << "\nTotal members (including President & Secretary): " << l->computeTotal();
break;
case 10: l->sortList();
break;
case 11: goto X;
break;
case 12: l1.concatList(l2);
break;
case 13: l->reverseDisplay();
break;
default: cout << "Wrong choice";
}
} while (choice != 0);
cout << "\n=============== GOOD BYE ===============\n";
return 0;
}