/***This is student linked list with their courses that they are registered in
The user can do different function on it (add students with list registered courses for each student,
delete by student's unique id,search by his unique id,print &count students in student list).
***/
#include <iostream>
#include <string>
using namespace std;
struct Course // represent course node
{
string subjectName;
float total;
float points;
char GPA;
Course *cprev;
Course *cnext;
};
struct Students //represent student's node
{
static int id;
int student_ID; //Unique ID for each student
string studentName;
string department;
int registeredCourses;
Course *courses;
Students * next;
Students* prev;
};
int Students::id=0;
Course *addCourse(Course ** courselist,string courseName, float grade,float points_,char gpa) //create course's list.
{
Course * newCourse;
newCourse= new Course;
newCourse->subjectName=courseName;
newCourse->total=grade;
newCourse->points=points_;
newCourse->GPA=gpa;
newCourse->cnext = NULL;
newCourse->cprev = NULL;
Course * currentCourse = *courselist;
if(currentCourse== NULL)
*courselist = newCourse;
else
{
//add to back of course list
//find last Course in list
while(currentCourse->cnext != NULL)
currentCourse = currentCourse->cnext;
//add new course to end of list;
currentCourse->cnext = newCourse;
newCourse->cprev = currentCourse;
}
return *courselist; //to get list of courses
}
void addStudents(Students ** studentList,Course *courselist,string name,string dept,int registerCourses) //add students to student's list with their course's list
{
Students * newStudent = new Students;
newStudent->courses= NULL;
newStudent->next = NULL;
newStudent->prev = NULL;
newStudent->id++;
newStudent->student_ID=newStudent->id;
newStudent->studentName=name;
newStudent->department=dept;
newStudent->registeredCourses=registerCourses;
Course * currentCourse ;
currentCourse=courselist;
while(currentCourse!=NULL)
{
Course * newCourse = new Course;
newCourse->subjectName= currentCourse->subjectName;
newCourse->total= currentCourse->total;
newCourse->points=currentCourse->points;
newCourse->GPA=currentCourse->GPA;
newCourse->cnext = NULL;
newCourse->cprev = NULL;
if(newStudent->courses==NULL)
{
newStudent->courses=newCourse;
}
else
{
//find the end of the list of courses in students
Course * tempcourse = newStudent->courses;
while(tempcourse->cnext != NULL)
tempcourse = tempcourse->cnext;
//add the new course to the end of the list in this student
tempcourse->cnext = newCourse;
newCourse->cprev = tempcourse;
}
currentCourse = currentCourse->cnext;
}
//add this student to the list of students
if(*studentList == NULL)
{
*studentList = newStudent;
}
else
{
//add to end of list
Students* currentStudent = *studentList;
while(currentStudent->next != NULL)
currentStudent = currentStudent->next;
currentStudent->next = newStudent;
newStudent->prev = currentStudent;
}
}
void displayStudents(Students *students) //show all student's data
{
Students *currStud = students;
if(currStud==NULL)
{
cout<< "Empty List"<<endl;
return;
}
while(currStud!=NULL)
{
cout << "The student's data"<< endl;
cout<< "---------------------"<<endl;
cout<< "Student's ID: "<< currStud->student_ID<<" ,";
cout <<"Name: "<<currStud->studentName<<" ,";
cout<< "Department: "<<currStud->department<<" ,";
cout<< "Register in ("<< currStud->registeredCourses<< ") courses."<<endl;
cout << "Courses :"<<endl;
Course *temp=currStud->courses;
while(temp!=NULL)
{
cout <<"course : "<< temp->subjectName<< " ,Total grade: "<< temp->total<< " , points: "<< temp->points<< " , Grade: "<< temp->GPA<<"."<<endl;
temp=temp->cnext;
}
cout << endl;
currStud=currStud->next;
}
}
void Delete_by_id(int i,Students* &students)
{
Students* temp=students;
// temp=head;
if(temp==NULL)
{
cout<<"You cannot delete because the list is empty"<<endl;
return ;
}
while(temp)
{
if(temp->student_ID==i)
{
if(temp->prev==NULL && temp->next==NULL) // delete list with only one student
{
delete(temp);
students =NULL;
cout << "Ok ,This student has been deleted. "<< endl;
return;
}
else if(temp->prev==NULL && temp->next!=NULL) //delete first student
{
temp->next->prev=NULL;
delete(temp);
cout << "Ok ,This student has been deleted. "<< endl;
return ;
}
else if(temp->prev!=NULL && temp->next==NULL) //delete last student
{
temp->prev->next=NULL;
delete(temp);
cout << "Ok ,This student has been deleted. "<< endl;
return ;
}
else // delete any middle position
{
temp->prev->next=temp->next;
temp->next->prev=temp->prev;
delete(temp);
cout << "Ok ,This student has been deleted. "<< endl;
return ;
}
}
else
temp=temp->next;
}
if(temp==NULL)
{
cout<<"Error! this id is not here...."<<endl;
return ;
}
}
void search_by_id(int i,Students* student)
{
Students* temp1=student;
if(temp1==NULL)
{
cout << "Sorry,Empty list ...!"<<endl;
return;
}
//temp1=head;
while(temp1)
{
if(temp1->student_ID==i)
{
cout<<" Search result: "<<endl;
cout << "\nThe student's data"<< endl;
cout<< "---------------------"<<endl;
cout<< "Student's ID: "<< temp1->student_ID<<endl;
cout <<"Name: "<<temp1->studentName<<endl;
cout<< "Department: "<<temp1->department<<endl;
cout<< "Register in ("<< temp1->registeredCourses<< ") courses."<<endl;
cout << "student'courses data:"<<endl;
Course *temp=temp1->courses;
while(temp!=NULL)
{
cout <<"course : "<< temp->subjectName<< " ,Total grade: "<< temp->total<< " , points: "<< temp->points<< " , Grade: "<< temp->GPA<<"."<<endl;
temp=temp->cnext;
}
cout << endl;
return ;
}
else
temp1=temp1->next;
}
if(temp1==NULL)
{
cout<<"Error! this id is not here...."<<endl;
return ;
}
return ;
}
void delete_list(Students* &student) // delete all members in the list.
{
student = NULL;
cout<<"your list is deleted !"<<endl;
}
int totalStudents(Students* student) // to get total students in the list
{
int t=0;
Students* temp=student;
while(temp)
{
t++;
temp=temp->next;
}
return t;
}
int main()
{
Course * courselist1 = NULL; // 7 course lists for different 7 students
Course * courselist2 = NULL;
Course * courselist3 = NULL;
Course * courselist4 = NULL;
Course * courselist5 = NULL;
Course * courselist6 = NULL;
Course * courselist7 = NULL;
*addCourse(&courselist1,"Data Structure",90,4,'A');
*addCourse(&courselist1,"Data Science",84,3.3,'B');
*addCourse(&courselist1,"Data Base 1",88,3.7,'A');
*addCourse(&courselist2,"Network",83,3.3,'B');
*addCourse(&courselist2,"Signal",72,3.1,'C');
*addCourse(&courselist2,"MultiMedia",66,2.5,'C');
*addCourse(&courselist2,"Security",88,3.7,'A');
*addCourse(&courselist3,"Data Base 2",98,4,'A');
*addCourse(&courselist3,"Data Science",88,3.7,'A');
*addCourse(&courselist3,"Data Information",77,3.0,'B');
*addCourse(&courselist4,"Data structure",84,3.7,'A');
*addCourse(&courselist4,"Algorithms",82,3.3,'B');
*addCourse(&courselist4,"Software Engineering",70,3.1,'C');
*addCourse(&courselist4,"Architecture",55,2.1,'D');
*addCourse(&courselist4,"Python Applications",92,4,'A');
*addCourse(&courselist5,"Management",89,3.7,'A');
*addCourse(&courselist5,"Modeling",77,3.1,'B');
*addCourse(&courselist6,"Image Processing",66,2.4,'C');
*addCourse(&courselist7,"Excel",92,4,'A');
*addCourse(&courselist7,"Simulation",85,3.7,'A');
*addCourse(&courselist7,"Data Base 2",78,3.3,'B');
*addCourse(&courselist7,"Data Science",67,2.5,'C');
Students* student1 = NULL; // student linked list
cout<<"\n***********************************************************************************************"<<endl;
cout<<"\n****** STUDENTS LIST WITH THEIR COURSES *****"<<endl;
cout<<"\n***********************************************************************************************"<<endl;
cout<<"\n** Add first 3 students **"<<endl;
addStudents(&student1,courselist1,"AYTEN SABER","CS",3);
addStudents(&student1,courselist2,"YASMIN ALI","IT",4);
addStudents(&student1,courselist3,"AHMAD FAHD","IS",3);
cout<<"\n\n** Show List After add 3 students **"<<endl;
displayStudents(student1);
cout<<"\n\n** Now delete the second student by it's unique id (2) **"<<endl;
Delete_by_id(2,student1);
cout<<"\n\n** View list again **"<<endl;
displayStudents(student1);
cout<<"\n\n** Destroy list **"<<endl;
delete_list(student1);
cout<<"\n\n** View list again **"<<endl;
displayStudents(student1);
cout<<"\n\n** Add other 4 Students **"<<endl;
addStudents(&student1,courselist4,"AFNAN HASAN","CS",5);
addStudents(&student1,courselist5,"NAGWA ZYAD","DS",2);
addStudents(&student1,courselist6,"ALAA HASSAN","AI",1);
addStudents(&student1,courselist7,"MOHANED TALAAT","DS",4);
cout<<"\n** Count of Students Now **"<<endl;
cout << "TOTAL STUDENTS NOW: (" << totalStudents(student1)<<") students."<<endl;
cout<<"\n\n** View list again **"<<endl;
displayStudents(student1);
cout<<"\n\n** Search the student with id=4 **\n"<<endl;
search_by_id(4,student1);
cout<<"\n\n** Delete Student with id=6 **"<<endl;
Delete_by_id(6,student1);
cout<<"\n\n** Search the student with id=6 **"<<endl;
search_by_id(6,student1);
cout<<"\n\n** Search the student with id=1 **"<<endl;
search_by_id(1,student1);
cout<<"\n\n** View list Now **\n"<<endl;
displayStudents(student1);
cout<<"\n** Thanks **"<<endl;
cout<<"***************************************"<<endl;
cout<<"***************************************"<<endl;
return 0;
}