@zareen
100% Solution Code
#include <iostream>
using namespace std;
/* The Student class */
class Student {
private:
string firstname, lastname, VUID;
int marks;
Student *nextStudent;
public:
// constructor of Student class to initialize data members of class
Student(){
VUID = "";
marks = 0;
firstname = "";
lastname = "";
nextStudent = NULL;
}
// Student class method to set VU ID of Student
void setVUID(string val){
VUID = val;
};
//Student class method to get VU ID of Student
string getVUID(){
return VUID;
};
// Student class method to set first name of Student
void setFirstName(string val){
firstname = val;
};
//Student class method to get first name of Student
string getFirstName(){
return firstname;
};
// Student class method to set last name of Student
void setLastName(string val){
lastname = val;
};
//Student class method to get last name of Student
string getLastName(){
return lastname;
};
//Student class method to set the Marks of Student
void setMarks(int val) {
marks = val;
};
// Student class method to get the Marks of Student
int getMarks() {
return marks;
}
//Student class method to point current Student to next Student
void setNext(Student *nextStudent) {
this->nextStudent = nextStudent;
}
// Student class method to get memory address where pointer is pointing
Student *getNext() {
return nextStudent;
}
};
/* The List class */
class List {
private:
Student * head;
Student * current;
public:
// constructor of list class to initialize data members of class
List() {
head = new Student();
head->setNext(NULL);
current = NULL;
}
// list class method to add Students into list
void add() {
Student *newStudent = new Student();
int loc_marks = 0;
string loc_vuid = "", loc_fname = "", loc_lname = "";
cout<<"\nEnter VU ID: ";
cin>>loc_vuid;
newStudent->setVUID(loc_vuid);
cout<<"Enter Marks: ";
cin>>loc_marks;
newStudent->setMarks(loc_marks);
cout<<"Enter First Name: ";
cin>>loc_fname;
newStudent->setFirstName(loc_fname);
cout<<"Enter Last Name: ";
cin>>loc_lname;
newStudent->setLastName(loc_lname);
if(head->getNext() == NULL){
newStudent->setNext(NULL);
head->setNext(newStudent);
current = newStudent;
}
else{
Student *temp = head;
while(temp->getNext() != NULL && temp->getNext()->getMarks() >= loc_marks){
temp = temp->getNext();
}
current = temp;
newStudent->setNext(current->getNext());
current->setNext( newStudent );
current = newStudent;
}
};
// list class method to get the information of Student
void getInfo() {
if (current != NULL){
cout<<"VU ID: "<<current->getVUID()<<endl;
cout<<"Marks: "<<current->getMarks()<<endl;
cout<<"First Name: "<<current->getFirstName()<<endl;
cout<<"Last Name: "<<current->getLastName()<<endl<<endl;
}
};
// list class method to move current to next Student
bool next() {
if (current == NULL){
return false;
}
current = current->getNext();
};
// frient function to list class to show all students in the list
friend void showStudents(List list){
Student* savedCurrent = list.current;
list.current = list.head;
for(int i = 1; list.next(); i++){
list.getInfo();
}
list.current = savedCurrent;
};
};
main() {
int input = 0;
List lst;
while(input != -1) {
cout<<"1. To Add New Student in Ranking"<<endl;
cout<<"2. To Display Ranking"<<endl;
cout<<"3. To Close"<<endl<<endl;
cout<<"Enter Your Choice: (1, 2 or 3) ";
cin>> input;
if(input == 1) {
lst.add();
cout<<"Student's information saved successfully.\n";
}
else if(input == 2) {
cout<<"\nRanking Chart"<<endl;
showStudents(lst);
return 0;
}
else {
return 0;
}
}
}