fork download
  1. /***This is student linked list with their courses that they are registered in
  2. The user can do different function on it (add students with list registered courses for each student,
  3. delete by student's unique id,search by his unique id,print &count students in student list).
  4. ***/
  5.  
  6.  
  7. #include <iostream>
  8. #include <string>
  9.  
  10. using namespace std;
  11.  
  12. struct Course // represent course node
  13. {
  14. string subjectName;
  15. float total;
  16. float points;
  17. char GPA;
  18. Course *cprev;
  19. Course *cnext;
  20. };
  21.  
  22. struct Students //represent student's node
  23. {
  24. static int id;
  25. int student_ID; //Unique ID for each student
  26. string studentName;
  27. string department;
  28. int registeredCourses;
  29. Course *courses;
  30. Students * next;
  31. Students* prev;
  32. };
  33. int Students::id=0;
  34.  
  35. Course *addCourse(Course ** courselist,string courseName, float grade,float points_,char gpa) //create course's list.
  36. {
  37.  
  38. Course * newCourse;
  39. newCourse= new Course;
  40. newCourse->subjectName=courseName;
  41. newCourse->total=grade;
  42. newCourse->points=points_;
  43. newCourse->GPA=gpa;
  44. newCourse->cnext = NULL;
  45. newCourse->cprev = NULL;
  46. Course * currentCourse = *courselist;
  47. if(currentCourse== NULL)
  48. *courselist = newCourse;
  49. else
  50. {
  51. //add to back of course list
  52. //find last Course in list
  53. while(currentCourse->cnext != NULL)
  54. currentCourse = currentCourse->cnext;
  55. //add new course to end of list;
  56. currentCourse->cnext = newCourse;
  57. newCourse->cprev = currentCourse;
  58. }
  59. return *courselist; //to get list of courses
  60. }
  61. void addStudents(Students ** studentList,Course *courselist,string name,string dept,int registerCourses) //add students to student's list with their course's list
  62. {
  63.  
  64. Students * newStudent = new Students;
  65. newStudent->courses= NULL;
  66. newStudent->next = NULL;
  67. newStudent->prev = NULL;
  68. newStudent->id++;
  69. newStudent->student_ID=newStudent->id;
  70. newStudent->studentName=name;
  71. newStudent->department=dept;
  72. newStudent->registeredCourses=registerCourses;
  73. Course * currentCourse ;
  74. currentCourse=courselist;
  75. while(currentCourse!=NULL)
  76. {
  77. Course * newCourse = new Course;
  78. newCourse->subjectName= currentCourse->subjectName;
  79. newCourse->total= currentCourse->total;
  80. newCourse->points=currentCourse->points;
  81. newCourse->GPA=currentCourse->GPA;
  82.  
  83. newCourse->cnext = NULL;
  84. newCourse->cprev = NULL;
  85. if(newStudent->courses==NULL)
  86. {
  87. newStudent->courses=newCourse;
  88.  
  89. }
  90. else
  91. {
  92. //find the end of the list of courses in students
  93. Course * tempcourse = newStudent->courses;
  94. while(tempcourse->cnext != NULL)
  95. tempcourse = tempcourse->cnext;
  96. //add the new course to the end of the list in this student
  97. tempcourse->cnext = newCourse;
  98. newCourse->cprev = tempcourse;
  99. }
  100. currentCourse = currentCourse->cnext;
  101. }
  102. //add this student to the list of students
  103. if(*studentList == NULL)
  104. {
  105. *studentList = newStudent;
  106. }
  107. else
  108. {
  109. //add to end of list
  110. Students* currentStudent = *studentList;
  111. while(currentStudent->next != NULL)
  112. currentStudent = currentStudent->next;
  113.  
  114. currentStudent->next = newStudent;
  115. newStudent->prev = currentStudent;
  116. }
  117. }
  118.  
  119. void displayStudents(Students *students) //show all student's data
  120. {
  121. Students *currStud = students;
  122. if(currStud==NULL)
  123. {
  124. cout<< "Empty List"<<endl;
  125. return;
  126. }
  127. while(currStud!=NULL)
  128. {
  129. cout << "The student's data"<< endl;
  130. cout<< "---------------------"<<endl;
  131. cout<< "Student's ID: "<< currStud->student_ID<<" ,";
  132. cout <<"Name: "<<currStud->studentName<<" ,";
  133. cout<< "Department: "<<currStud->department<<" ,";
  134. cout<< "Register in ("<< currStud->registeredCourses<< ") courses."<<endl;
  135. cout << "Courses :"<<endl;
  136.  
  137. Course *temp=currStud->courses;
  138. while(temp!=NULL)
  139. {
  140. cout <<"course : "<< temp->subjectName<< " ,Total grade: "<< temp->total<< " , points: "<< temp->points<< " , Grade: "<< temp->GPA<<"."<<endl;
  141. temp=temp->cnext;
  142.  
  143. }
  144. cout << endl;
  145. currStud=currStud->next;
  146. }
  147. }
  148. void Delete_by_id(int i,Students* &students)
  149. {
  150. Students* temp=students;
  151. // temp=head;
  152. if(temp==NULL)
  153. {
  154. cout<<"You cannot delete because the list is empty"<<endl;
  155. return ;
  156. }
  157. while(temp)
  158. {
  159. if(temp->student_ID==i)
  160. {
  161. if(temp->prev==NULL && temp->next==NULL) // delete list with only one student
  162. {
  163. delete(temp);
  164. students =NULL;
  165. cout << "Ok ,This student has been deleted. "<< endl;
  166. return;
  167.  
  168. }
  169. else if(temp->prev==NULL && temp->next!=NULL) //delete first student
  170. {
  171. temp->next->prev=NULL;
  172. delete(temp);
  173. cout << "Ok ,This student has been deleted. "<< endl;
  174. return ;
  175. }
  176. else if(temp->prev!=NULL && temp->next==NULL) //delete last student
  177. {
  178. temp->prev->next=NULL;
  179. delete(temp);
  180. cout << "Ok ,This student has been deleted. "<< endl;
  181. return ;
  182. }
  183. else // delete any middle position
  184. {
  185. temp->prev->next=temp->next;
  186. temp->next->prev=temp->prev;
  187. delete(temp);
  188. cout << "Ok ,This student has been deleted. "<< endl;
  189. return ;
  190. }
  191.  
  192. }
  193. else
  194. temp=temp->next;
  195. }
  196. if(temp==NULL)
  197. {
  198. cout<<"Error! this id is not here...."<<endl;
  199. return ;
  200. }
  201. }
  202. void search_by_id(int i,Students* student)
  203. {
  204. Students* temp1=student;
  205. if(temp1==NULL)
  206. {
  207. cout << "Sorry,Empty list ...!"<<endl;
  208. return;
  209. }
  210. //temp1=head;
  211. while(temp1)
  212. {
  213. if(temp1->student_ID==i)
  214. {
  215. cout<<" Search result: "<<endl;
  216. cout << "\nThe student's data"<< endl;
  217. cout<< "---------------------"<<endl;
  218. cout<< "Student's ID: "<< temp1->student_ID<<endl;
  219. cout <<"Name: "<<temp1->studentName<<endl;
  220. cout<< "Department: "<<temp1->department<<endl;
  221. cout<< "Register in ("<< temp1->registeredCourses<< ") courses."<<endl;
  222. cout << "student'courses data:"<<endl;
  223. Course *temp=temp1->courses;
  224. while(temp!=NULL)
  225. {
  226. cout <<"course : "<< temp->subjectName<< " ,Total grade: "<< temp->total<< " , points: "<< temp->points<< " , Grade: "<< temp->GPA<<"."<<endl;
  227. temp=temp->cnext;
  228.  
  229. }
  230. cout << endl;
  231.  
  232. return ;
  233. }
  234. else
  235. temp1=temp1->next;
  236. }
  237. if(temp1==NULL)
  238. {
  239. cout<<"Error! this id is not here...."<<endl;
  240. return ;
  241. }
  242. return ;
  243. }
  244. void delete_list(Students* &student) // delete all members in the list.
  245. {
  246. student = NULL;
  247. cout<<"your list is deleted !"<<endl;
  248. }
  249. int totalStudents(Students* student) // to get total students in the list
  250. {
  251. int t=0;
  252. Students* temp=student;
  253. while(temp)
  254. {
  255. t++;
  256. temp=temp->next;
  257. }
  258. return t;
  259. }
  260. int main()
  261. {
  262.  
  263. Course * courselist1 = NULL; // 7 course lists for different 7 students
  264. Course * courselist2 = NULL;
  265. Course * courselist3 = NULL;
  266. Course * courselist4 = NULL;
  267. Course * courselist5 = NULL;
  268. Course * courselist6 = NULL;
  269. Course * courselist7 = NULL;
  270. *addCourse(&courselist1,"Data Structure",90,4,'A');
  271. *addCourse(&courselist1,"Data Science",84,3.3,'B');
  272. *addCourse(&courselist1,"Data Base 1",88,3.7,'A');
  273.  
  274. *addCourse(&courselist2,"Network",83,3.3,'B');
  275. *addCourse(&courselist2,"Signal",72,3.1,'C');
  276. *addCourse(&courselist2,"MultiMedia",66,2.5,'C');
  277. *addCourse(&courselist2,"Security",88,3.7,'A');
  278.  
  279. *addCourse(&courselist3,"Data Base 2",98,4,'A');
  280. *addCourse(&courselist3,"Data Science",88,3.7,'A');
  281. *addCourse(&courselist3,"Data Information",77,3.0,'B');
  282.  
  283. *addCourse(&courselist4,"Data structure",84,3.7,'A');
  284. *addCourse(&courselist4,"Algorithms",82,3.3,'B');
  285. *addCourse(&courselist4,"Software Engineering",70,3.1,'C');
  286. *addCourse(&courselist4,"Architecture",55,2.1,'D');
  287. *addCourse(&courselist4,"Python Applications",92,4,'A');
  288.  
  289. *addCourse(&courselist5,"Management",89,3.7,'A');
  290. *addCourse(&courselist5,"Modeling",77,3.1,'B');
  291.  
  292. *addCourse(&courselist6,"Image Processing",66,2.4,'C');
  293.  
  294. *addCourse(&courselist7,"Excel",92,4,'A');
  295. *addCourse(&courselist7,"Simulation",85,3.7,'A');
  296. *addCourse(&courselist7,"Data Base 2",78,3.3,'B');
  297. *addCourse(&courselist7,"Data Science",67,2.5,'C');
  298. Students* student1 = NULL; // student linked list
  299. cout<<"\n***********************************************************************************************"<<endl;
  300. cout<<"\n****** STUDENTS LIST WITH THEIR COURSES *****"<<endl;
  301. cout<<"\n***********************************************************************************************"<<endl;
  302. cout<<"\n** Add first 3 students **"<<endl;
  303. addStudents(&student1,courselist1,"AYTEN SABER","CS",3);
  304. addStudents(&student1,courselist2,"YASMIN ALI","IT",4);
  305. addStudents(&student1,courselist3,"AHMAD FAHD","IS",3);
  306. cout<<"\n\n** Show List After add 3 students **"<<endl;
  307. displayStudents(student1);
  308. cout<<"\n\n** Now delete the second student by it's unique id (2) **"<<endl;
  309. Delete_by_id(2,student1);
  310. cout<<"\n\n** View list again **"<<endl;
  311. displayStudents(student1);
  312. cout<<"\n\n** Destroy list **"<<endl;
  313. delete_list(student1);
  314. cout<<"\n\n** View list again **"<<endl;
  315. displayStudents(student1);
  316. cout<<"\n\n** Add other 4 Students **"<<endl;
  317. addStudents(&student1,courselist4,"AFNAN HASAN","CS",5);
  318. addStudents(&student1,courselist5,"NAGWA ZYAD","DS",2);
  319. addStudents(&student1,courselist6,"ALAA HASSAN","AI",1);
  320. addStudents(&student1,courselist7,"MOHANED TALAAT","DS",4);
  321. cout<<"\n** Count of Students Now **"<<endl;
  322. cout << "TOTAL STUDENTS NOW: (" << totalStudents(student1)<<") students."<<endl;
  323. cout<<"\n\n** View list again **"<<endl;
  324. displayStudents(student1);
  325. cout<<"\n\n** Search the student with id=4 **\n"<<endl;
  326. search_by_id(4,student1);
  327. cout<<"\n\n** Delete Student with id=6 **"<<endl;
  328. Delete_by_id(6,student1);
  329. cout<<"\n\n** Search the student with id=6 **"<<endl;
  330. search_by_id(6,student1);
  331. cout<<"\n\n** Search the student with id=1 **"<<endl;
  332. search_by_id(1,student1);
  333. cout<<"\n\n** View list Now **\n"<<endl;
  334. displayStudents(student1);
  335. cout<<"\n** Thanks **"<<endl;
  336. cout<<"***************************************"<<endl;
  337. cout<<"***************************************"<<endl;
  338.  
  339.  
  340.  
  341. return 0;
  342. }
  343.  
  344.  
Success #stdin #stdout 0s 4400KB
stdin
Standard input is empty
stdout
***********************************************************************************************

******                   STUDENTS LIST WITH THEIR COURSES                                 *****

***********************************************************************************************

** Add first 3 students  **


** Show List After add 3 students   **
The student's data
---------------------
Student's ID:  1 ,Name: AYTEN SABER ,Department: CS ,Register in (3) courses.
Courses :
course : Data Structure ,Total grade: 90 , points: 4 , Grade: A.
course : Data Science ,Total grade: 84 , points: 3.3 , Grade: B.
course : Data Base 1 ,Total grade: 88 , points: 3.7 , Grade: A.

The student's data
---------------------
Student's ID:  2 ,Name: YASMIN ALI ,Department: IT ,Register in (4) courses.
Courses :
course : Network ,Total grade: 83 , points: 3.3 , Grade: B.
course : Signal ,Total grade: 72 , points: 3.1 , Grade: C.
course : MultiMedia ,Total grade: 66 , points: 2.5 , Grade: C.
course : Security ,Total grade: 88 , points: 3.7 , Grade: A.

The student's data
---------------------
Student's ID:  3 ,Name: AHMAD FAHD ,Department: IS ,Register in (3) courses.
Courses :
course : Data Base 2 ,Total grade: 98 , points: 4 , Grade: A.
course : Data Science ,Total grade: 88 , points: 3.7 , Grade: A.
course : Data Information ,Total grade: 77 , points: 3 , Grade: B.



** Now delete the second student by it's unique id (2) **
Ok ,This student has been deleted. 


**  View list again **
The student's data
---------------------
Student's ID:  1 ,Name: AYTEN SABER ,Department: CS ,Register in (3) courses.
Courses :
course : Data Structure ,Total grade: 90 , points: 4 , Grade: A.
course : Data Science ,Total grade: 84 , points: 3.3 , Grade: B.
course : Data Base 1 ,Total grade: 88 , points: 3.7 , Grade: A.

The student's data
---------------------
Student's ID:  3 ,Name: AHMAD FAHD ,Department: IS ,Register in (3) courses.
Courses :
course : Data Base 2 ,Total grade: 98 , points: 4 , Grade: A.
course : Data Science ,Total grade: 88 , points: 3.7 , Grade: A.
course : Data Information ,Total grade: 77 , points: 3 , Grade: B.



**  Destroy list **
your list is deleted !


** View list again  **
Empty List


**  Add other 4 Students   **

** Count of Students Now   **
TOTAL STUDENTS NOW: (4) students.


** View list again  **
The student's data
---------------------
Student's ID:  4 ,Name: AFNAN HASAN ,Department: CS ,Register in (5) courses.
Courses :
course : Data structure ,Total grade: 84 , points: 3.7 , Grade: A.
course : Algorithms ,Total grade: 82 , points: 3.3 , Grade: B.
course : Software Engineering ,Total grade: 70 , points: 3.1 , Grade: C.
course : Architecture ,Total grade: 55 , points: 2.1 , Grade: D.
course : Python Applications ,Total grade: 92 , points: 4 , Grade: A.

The student's data
---------------------
Student's ID:  5 ,Name: NAGWA ZYAD ,Department: DS ,Register in (2) courses.
Courses :
course : Management ,Total grade: 89 , points: 3.7 , Grade: A.
course : Modeling ,Total grade: 77 , points: 3.1 , Grade: B.

The student's data
---------------------
Student's ID:  6 ,Name: ALAA HASSAN ,Department: AI ,Register in (1) courses.
Courses :
course : Image Processing ,Total grade: 66 , points: 2.4 , Grade: C.

The student's data
---------------------
Student's ID:  7 ,Name: MOHANED TALAAT ,Department: DS ,Register in (4) courses.
Courses :
course : Excel ,Total grade: 92 , points: 4 , Grade: A.
course : Simulation ,Total grade: 85 , points: 3.7 , Grade: A.
course : Data Base 2 ,Total grade: 78 , points: 3.3 , Grade: B.
course : Data Science ,Total grade: 67 , points: 2.5 , Grade: C.



** Search the student with id=4   **

 Search result: 

The student's data
---------------------
Student's ID:  4
Name: AFNAN HASAN
Department: CS
Register in (5) courses.
student'courses data:
course : Data structure ,Total grade: 84 , points: 3.7 , Grade: A.
course : Algorithms ,Total grade: 82 , points: 3.3 , Grade: B.
course : Software Engineering ,Total grade: 70 , points: 3.1 , Grade: C.
course : Architecture ,Total grade: 55 , points: 2.1 , Grade: D.
course : Python Applications ,Total grade: 92 , points: 4 , Grade: A.



**  Delete Student with id=6  **
Ok ,This student has been deleted. 


** Search the student with id=6  **
Error! this id is not here....


** Search the student with id=1  **
Error! this id is not here....


**  View list Now **

The student's data
---------------------
Student's ID:  4 ,Name: AFNAN HASAN ,Department: CS ,Register in (5) courses.
Courses :
course : Data structure ,Total grade: 84 , points: 3.7 , Grade: A.
course : Algorithms ,Total grade: 82 , points: 3.3 , Grade: B.
course : Software Engineering ,Total grade: 70 , points: 3.1 , Grade: C.
course : Architecture ,Total grade: 55 , points: 2.1 , Grade: D.
course : Python Applications ,Total grade: 92 , points: 4 , Grade: A.

The student's data
---------------------
Student's ID:  5 ,Name: NAGWA ZYAD ,Department: DS ,Register in (2) courses.
Courses :
course : Management ,Total grade: 89 , points: 3.7 , Grade: A.
course : Modeling ,Total grade: 77 , points: 3.1 , Grade: B.

The student's data
---------------------
Student's ID:  7 ,Name: MOHANED TALAAT ,Department: DS ,Register in (4) courses.
Courses :
course : Excel ,Total grade: 92 , points: 4 , Grade: A.
course : Simulation ,Total grade: 85 , points: 3.7 , Grade: A.
course : Data Base 2 ,Total grade: 78 , points: 3.3 , Grade: B.
course : Data Science ,Total grade: 67 , points: 2.5 , Grade: C.


** Thanks  **
***************************************
***************************************