-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiLinkedlist.cpp
More file actions
491 lines (412 loc) · 14.9 KB
/
MultiLinkedlist.cpp
File metadata and controls
491 lines (412 loc) · 14.9 KB
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
//PROGRAM FOR COURSE REGISTRATION
//function prototypes
void AddCourse(); //done
void DeleteCourse(); //done
void SearchCourse(); //done
void PrintCourse(); //done
void AddStudent(); //done
void DeleteAllStudents(); //used in deletecourse to delete all students before deleting a course
void DeleteStudentfromCourse(); //done
void DeleteStudentCompletely();
void SearchStudent();
void FullSearchStudent();
void PrintStudent();
#include<iostream>
using namespace std;
//structs defined
struct Student //list of students
{
string Name;
string Seatno; //seat number of student like b23001
Student* nextstudent; //pointer for next student in list
};
struct Course //course list
{
string CourseNo; //the course number like cs-451
string CourseName;
Student* Studentlist; //list of the students which will be attached to each course(nested list)
Course* nextcourse; //pointer to next course
};
Course* coursehead = NULL; //global initial pointer to course which is now empty
//function implementation
void AddCourse()
{
Course* course = new Course();
string coursenumber, coursename;
cout<< "Enter the course number:\t";
cin>> coursenumber;
cout<< "Enter the course name:\t";
cin>> coursename;
course->CourseNo = coursenumber;
course->CourseName = coursename;
course->Studentlist = NULL;
course->nextcourse= NULL; //initially the next course is null
if(coursehead==NULL) //if the course list is empty then assign the first added course as head
{
coursehead = course;
}
else
{
Course* temp= coursehead;
while(temp->nextcourse!=NULL)
{
temp=temp->nextcourse; //traverse till end
}
temp->nextcourse=course; //place the newly added course to the end
}
cout<< "course added successfully!" <<endl;
}
void DeleteAllStudents(Student* studentlist) //WHILE REVISING CHECK HERE WHY ENTIRE STUDENTLIST IS PASSED?
{
Student* temp;
while (studentlist != NULL)
{
temp = studentlist; //save the first node or the headnode of the student list
studentlist = studentlist->nextstudent; //traverse karte jao agey barhte jao
delete temp; // ek ek student ko delete krte jao
}
}
void DeleteCourse()
{
string input; //deletion condiion will work on either of the two if the name or number is entered then delete that specific course
cout<< "Enter the COURSE NAME or COURSE NUMBER to delete:\t";
cin.ignore();
getline(cin,input);
//if there us one course then delete it
Course* temp = coursehead;
Course* prev = NULL;
if(temp==NULL) //if there is no course
{
cout<< "No course to delete!" << endl;
return;
}
//if the course to be deleted is the first node(head)
if(temp->CourseName == input || temp->CourseNo== input)
{
coursehead= temp->nextcourse; //update the coursehead to next course before deleteing it to avoid losing the entire list
DeleteAllStudents(temp->Studentlist); //delete all students before deleting teh course bcx if course doesnt exist the student should also not be registered to it
delete temp;
cout<< "Course list is now empty after deletion" << endl;
return;
}
//if there is a certain course then first search it and then delete it
while (temp->CourseName != input && temp->CourseNo!= input && temp!=NULL) { //traverse the list and search
prev = temp; //keep saving current nodes while moving forward (Because we move first, so we must save prev before losing reference.)
temp=temp->nextcourse;
}
//case: if course found somewhere in list
prev->nextcourse= temp->nextcourse; //skip the node to delete and link the prev node to the next node of the one to be deleted
DeleteAllStudents(temp->Studentlist);
delete temp;
cout<< "Course deleted succesfully!";
}
void SearchCourse() //to search and display a specific course
{
string input;
cout<< "Enter the COURSE NAME or COURSE NUMBER to search:\t";
cin.ignore();
getline(cin,input);
Course* temp = coursehead; //assign the first headnode course to temp for traversal
bool found = false;
while(temp!=NULL) //traverse till end
{
if((temp->CourseName==input) || (temp->CourseNo==input))
{
cout<< "Course " << temp->CourseName << " (" << temp->CourseNo << ") found in the courses list!" << endl;
found = true;
}
temp=temp->nextcourse; //keep traversing
}
if(!found){
cout<< "Course not found in the list!" << endl;
}
}
void PrintCourse()
{
cout<< "***LIST OF COURSES***" << endl;
Course* temp = coursehead;
while(temp!=NULL)
{
cout << temp->CourseName << " (" << temp->CourseNo << ")"<< endl;
temp=temp->nextcourse;
}
}
void AddStudent()
{
Student* student = new Student();
string name, seatnumber , inputcourse;
cout<< "Enter the name of the student:\t";
cin>> name;
cout<< "Enter the seat number of the student:\t";
cin>> seatnumber;
cout<< "Which course name or number to register with ?:\t";
cin>> inputcourse;
//searching for code
Course* coursetemp = coursehead;
while(coursetemp!=NULL)
{
if((coursetemp->CourseName== inputcourse ) || (coursetemp->CourseNo== inputcourse))
{
break; //course found
}
coursetemp=coursetemp->nextcourse;
}
//if code does not exist then return and delete the student
if(coursetemp==NULL)
{
cout << "Course does not exist : Cannot register student" << endl;
delete student; //delete the student to free the memory bcz course doesnt exist then a student cannot be registered
return;
}
student->Name = name;
student->Seatno = seatnumber;
student->nextstudent= NULL; //initially next student is null
if(coursetemp->Studentlist==NULL) //if the student list is empty then assign the first added student as head
{
coursetemp->Studentlist = student;
}
else
{
Student* temp= coursetemp->Studentlist; //studentlist of that particular course where student is being added (a pointer to the head of the student list in that course)
while(temp->nextstudent!=NULL)
{
temp=temp->nextstudent; //traverse till end
}
temp->nextstudent=student; //place the newly added student to the end
}
cout<< name << " " << seatnumber << " registered to the course " << coursetemp->CourseName <<endl;
}
void DeleteStudentfromCourse()
{
string inputstudent;
cout<< "Enter the NAME or SEAT NUMBER of the student to delete:\t";
cin.ignore();
getline(cin,inputstudent);
string inputcourse;
cout<< "Enter the COURSE NAME or COURSE NUMBER to delete:\t";
cin.ignore();
getline(cin,inputcourse);
Course* coursetemp = coursehead;
//find the course
while(coursetemp!=NULL) //keep traversing until u find that particular course
{
if((coursetemp->CourseName== inputcourse ) || (coursetemp->CourseNo== inputcourse))
{
break; //course found
}
coursetemp=coursetemp->nextcourse; //to move forward
}
if (coursetemp == NULL) //if the course was not found then return
{
cout << "Course not found.\n";
return;
}
// find the student inside the course
Student* studenttemp= coursetemp->Studentlist;
Student* prev= NULL;
while(studenttemp!=NULL) //keep traversing until u find the student
{
if((studenttemp->Name == inputstudent) || (studenttemp->Seatno==inputstudent))
{
if (prev==NULL) //if the student to be deleted is the first in the list then
{
coursetemp->Studentlist = studenttemp->nextstudent; //update the head node of student list by the next student in list of that course
}
else
{
prev->nextstudent= studenttemp->nextstudent; //otherwise link prev student to next
}
cout<< studenttemp->Name << " (" << studenttemp->Seatno << ") deleted from course " << coursetemp->CourseName << endl;
delete studenttemp; //and then delete that particular inputted student
return;
}
prev = studenttemp; //move the prev to current student (Because we check first, then move forward.)
studenttemp= studenttemp->nextstudent; //keep moving the student list
}
cout << "Student not found in the list!" << endl;
}
void DeleteStudentCompletely()
{
string inputstudent;
cout << "Enter the NAME or SEAT NUMBER of the student to delete:\t";
cin.ignore();
getline(cin, inputstudent);
Course* coursetemp = coursehead; // Start from first course
bool found = false; // To check if student was deleted from any course
while (coursetemp != NULL) // Traverse all courses
{
Student* studenttemp = coursetemp->Studentlist;
Student* prev = NULL;
while (studenttemp != NULL) // Traverse students in the course
{
if ((studenttemp->Name == inputstudent) || (studenttemp->Seatno == inputstudent))
{
if (prev == NULL) // If first student in the course
{
coursetemp->Studentlist = studenttemp->nextstudent; //update the head node of student list by the next student in list of that course
}
else
{
prev->nextstudent = studenttemp->nextstudent; //link the prev student with the next student and skip the one being deleted
}
cout << studenttemp->Name << " (" << studenttemp->Seatno << ") removed from " << coursetemp->CourseName << endl;
delete studenttemp;
found = true;
break; // Exit loop as deletion is done for this course
}
prev = studenttemp; //save current student in prev first check and then move forward
studenttemp = studenttemp->nextstudent; //keep movinf forward
}
coursetemp = coursetemp->nextcourse; // Move to next course for traversing all courses in the list
}
if (!found)
{
cout << "Student not found in any course!" << endl;
}
else
{
cout << "Student completely removed from all courses." << endl;
}
}
void SearchStudent() {
string inputcourse, inputstudent;
cout << "Enter the COURSE NAME or COURSE NUMBER: ";
cin.ignore();
getline(cin, inputcourse);
cout << "Enter the STUDENT NAME or SEAT NUMBER: ";
getline(cin, inputstudent);
Course* coursetemp = coursehead;
// Search for the course
while (coursetemp != NULL) {
if (coursetemp->CourseName == inputcourse || coursetemp->CourseNo == inputcourse) {
break; // Course found
}
coursetemp = coursetemp->nextcourse;
}
if (coursetemp == NULL) {
cout << "Course not found!\n";
return;
}
// Search for the student in the course's student list
Student* studenttemp = coursetemp->Studentlist; //point to the first student head in the specific course
while (studenttemp != NULL)
{
if (studenttemp->Name == inputstudent || studenttemp->Seatno == inputstudent) {
cout << "Student " << studenttemp->Name << " (" << studenttemp->Seatno << ") is enrolled in " << coursetemp->CourseName << ".\n";
return;
}
studenttemp = studenttemp->nextstudent;
}
cout << "Student not found in this course!\n";
}
void FullSearchStudent() {
string inputstudent;
cout << "Enter the STUDENT NAME or SEAT NUMBER: ";
cin.ignore();
getline(cin, inputstudent);
Course* coursetemp = coursehead;
bool found = false; //to track the student in courses
while (coursetemp != NULL) { //for each course check tthe student list inside this
Student* studenttemp = coursetemp->Studentlist;
while (studenttemp != NULL) { //traverse untill teh entire studentlist for that particular course
if (studenttemp->Name == inputstudent || studenttemp->Seatno == inputstudent) {
cout << "Student " << studenttemp->Name << " (" << studenttemp->Seatno << ") is enrolled in " << coursetemp->CourseName << ".\n";
found = true;
}
studenttemp = studenttemp->nextstudent; //keep student list moving forward
}
coursetemp = coursetemp->nextcourse; //keep courses moving forward
}
if (!found) {
cout << "Student not found in any course!\n";
}
}
void PrintStudentInCourse() { //this prints all students in a course
string inputcourse;
cout << "Enter the COURSE NAME or COURSE NUMBER: ";
cin.ignore();
getline(cin, inputcourse);
Course* coursetemp = coursehead;
// Search for the course
while (coursetemp != NULL) {
if (coursetemp->CourseName == inputcourse || coursetemp->CourseNo == inputcourse) {
break; // Course found
}
coursetemp = coursetemp->nextcourse;
}
if (coursetemp == NULL) {
cout << "Course not found!\n";
return;
}
// Print students in the course
Student* studenttemp = coursetemp->Studentlist;
if (studenttemp == NULL) { //if the studentlist is empty
cout << "No students enrolled in this course!\n";
return;
}
cout << "Students enrolled in " << coursetemp->CourseName << ":\n";
while (studenttemp != NULL) { //traverse the list and print the student name and seatno
cout << "- " << studenttemp->Name << " (" << studenttemp->Seatno << ")\n";
studenttemp = studenttemp->nextstudent;
}
}
int main()
{
cout<< "WELCOME TO THE COURSE REGISTRATION PROGRAM:\t";
int option;
do
{
cout << "\n1) Add a course\n"
<< "2) Delete a course\n"
<< "3) Search a course\n"
<< "4) Print all courses\n"
<< "5) Add a student to a course\n"
<< "6) Delete a student from a course\n"
<< "7) Delete a Student's Data Completely\n"
<< "8) Search a student\n"
<< "9) Search a student in all courses\n "
<< "10) Print all students in a course\n"
<< "11) Exit the program" << endl;
cout<< "\nChoose the action you want to perform:\n";
cin>> option;
switch(option)
{
case 1: //add a course
AddCourse();
break;
case 2: //delete a course
DeleteCourse();
break;
case 3: //search a course
SearchCourse();
break;
case 4: //print all courses
PrintCourse();
break;
case 5: //Add a student to a course
AddStudent();
break;
case 6: //delete a student from a course
DeleteStudentfromCourse();
break;
case 7: //delete a student( complete removal of data)
DeleteStudentCompletely();
break;
case 8: //search a student
SearchStudent();
break;
case 9: //search a student everywhere(in all courses enrolled)
FullSearchStudent();
break;
case 10: //print students in a course
PrintStudentInCourse();
break;
case 11: //exit the program
cout << "Program exited!" << endl;
break;
default: //invalid option entered
cout << "Invalid option! please enter a valid option" << endl;
}
}while(option!=11);
return 0;
}