Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In C++ your task is to complete program that maintains a linked list of students

ID: 3677623 • Letter: I

Question

In C++ your task is to complete program that maintains a linked list of students. You will use a struct called Student that contains a name, id, status and gpa as members. The program will read command codes and data from a file to process the list. Here is a list of the functionality your program will implement.

- add one student to the list

- remove one student from the list

- a listing of the students

- a listing of all of the student for a given classification (Senr, JunR, Soph, Fresh)

- a listing of all of the deanslist students (above 3.5 gpa)

- destroy the list of students upon program termination.

You should implement a solution to this problem using a linked list. Each node of the linked list should look like this:

struct Student { string Name; // Consist of the Name of the student integer Id; // Student ID string Status; // Example status include Fresh, Soph, Junr, or Senr double Gpa; // Grade point average Student* nextStudent; // Pointer to the next student };

The program will use a head pointer called FirstStud to point to the beginning of the list.

The pointer is set to NULL Declare two enumerated types: one for class rank (FRESH, SOPH, JUNR, SENR) and Selection of program operations (ADD, REMOVE, PRINTLIST, CLASSLIST, DEANSLIST, QUIT) Set enumerators appropriately so that Fresh =1, Soph = 2, etc. and program operations correspond to the data file numerical values.

The following functions are required

CreateStudent: Reads the next student record from the data file and assigns the values to newly allocated (Student structure) memory. Returns a pointer to that memory via the return statement. File object variable is passed to this function. Upon return, in main, capture the returned address into NewStud pointer [ This may be a call to another function! ].

RemoveStudent: Accept the FirstStud pointer. This function should read the ID from the data file and remove that student from the list and print a message indicating the results of the removal. The student may not be on the list.

PrintList: Accept the FirstStud pointer. Print all of the data about each student in tabular form.

ClassList: Accept the FirstStud pointer. This function should read in a numerical value for the classification from the data file and print a list of all of the students in that class in tabular form. (Senr = 4, JunR = 3, Soph = 2, Fresh = 1). If there are no students in that class, print a message indicating. The header will always be printed. DeansList: Accept the FirstStud pointer. Print all of the Dean’s List student in tabular form. (gpa of 3.5 or better) ClearList: Accept the FirstStud pointer. This function should remove all of the students from the list before the program terminates. The function will print the name of the student right before the student is removed. getClassName: Write a helper function that will receive class rank in numerical form and return “Freshman”, “Sophomore”, etc. via the return type. This function will be called from ClassList function so that an appropriate heading can be displayed above class listing. Function prototypes: Student* CreateStudent(ifstream &); void AddStudent(Student* &, Student*); void RemoveStudent(Student* &, ifstream &); void PrintList(Student*); void ClassList(Student*, ifstream &); void DeansList(Student*); void ClearList(Student* &); string getClassName(int); Use the getline() function to read in student name. Program Output Formatting Create appropriate headings over each section of output. Show student count, student name, Id, gpa, and class rank for general listing, show gpa for dean’s list, and class rank for class list.

Explanation / Answer

#include <bits/stdc++.h>
using namespace std;

enum CLASS { FRESH, SOPH, JUNR, SENR };
enum OPERATION { ADD, REMOVE, PRINTLIST, CLASSLIST, DEANSLIST, QUIT };

struct Student {
int Id; // Student ID
string Name; // Consist of the Name of the student
string Status; // Example status include Fresh, Soph, Junr, or Senr
double Gpa; // Grade point average
Student* nextStudent; // Pointer to the next student
};

string getClassName(int classIndex) {
switch (classIndex) {
case FRESH:
return "Freshman";
case SOPH:
return "Sophomore";
case JUNR:
return "Junior";
case SENR:
return "Senior";
default:
return "Not allowed";
}
}

Student* CreateStudent(ifstream &fp) {
Student* new_student = new Student();
cin >> new_student->Id;
getline(fp, new_student->Name);
getline(fp, new_student->Status);
cin >> new_student->Gpa;
new_student->nextStudent = NULL;
return new_student;
}

void AddStudent(Student* &FirstStud, Student* NewStudent) {
if (FirstStud == NULL) {
FirstStud = NewStudent;
} else {
NewStudent->nextStudent = FirstStud;
FirstStud = NewStudent;
}
}

void RemoveStudent(Student* &FirstStud, ifstream &fp) {
int IdOfStudentToDelete;
fp >> IdOfStudentToDelete;

Student* it = FirstStud;
Student* prvs = NULL;

while (it != NULL && it->Id != IdOfStudentToDelete) {
prvs = it;
it = it->nextStudent;
}

if (it != NULL) {
if (prvs == NULL) {
FirstStud = FirstStud->nextStudent;
} else {
prvs->nextStudent = it->nextStudent;
}
delete it;
}
}

void PrintList(Student* FirstStud) {
printf("%10s %20s %10s %5s ", "Id", "Name", "Status", "GPA");
while (FirstStud != NULL) {
cout << setw(10) << FirstStud->Id;
cout << ' ' << setw(20) << FirstStud->Name;
cout << ' ' << setw(10) << FirstStud->Status;
cout << ' ' << setw(5) << FirstStud->Gpa << endl;
FirstStud = FirstStud->nextStudent;
}
}

void ClassList(Student* FirstStud, ifstream &fp) {
int classificationCriteria;
fp >> classificationCriteria;

string className = getClassName(classificationCriteria);

printf("%10s %20s %10s %5s ", "Id", "Name", "Status", "GPA");
bool ok = false;
while (FirstStud != NULL) {
if (FirstStud->Status == className) {
cout << setw(10) << FirstStud->Id;
cout << ' ' << setw(20) << FirstStud->Name;
cout << ' ' << setw(10) << FirstStud->Status;
cout << ' ' << setw(5) << FirstStud->Gpa << endl;
ok = true;
}
FirstStud = FirstStud->nextStudent;
}

if (!ok) cout << "There are no students in this class!" << endl;
}

void DeansList(Student* FirstStud) {

printf("%10s %20s %10s %5s ", "Id", "Name", "Status", "GPA");
bool ok = false;
while (FirstStud != NULL) {
if (FirstStud->Gpa >= 3.5) {
cout << setw(10) << FirstStud->Id;
cout << ' ' << setw(20) << FirstStud->Name;
cout << ' ' << setw(10) << FirstStud->Status;
cout << ' ' << setw(5) << FirstStud->Gpa << endl;
ok = true;
}
FirstStud = FirstStud->nextStudent;
}

if (!ok) cout << "There are no students in deans list!" << endl;
}

void ClearList(Student* &FirstStud) {
if (FirstStud == NULL) return;
else {
ClearList(FirstStud->nextStudent);
delete FirstStud;
FirstStud = NULL;
}
}

void printMenu() {
cout << "0. Add student" << endl;
cout << "1. Remove student" << endl;
cout << "2. Print list" << endl;
cout << "3. Class list" << endl;
cout << "4. Dean list" << endl;
cout << "5. Quit" << endl;
}

void getInputStream(ifstream &fp) {
string file_name;
cout << "Enter file name: ";
cin >> file_name;
fp.open(file_name.c_str());
}

int main() {
Student* FirstStud = NULL;
while (true) {
printMenu();
int userChoice;
cin >> userChoice;
ifstream fp;
switch (userChoice) {
case ADD:
getInputStream(fp);
if (!fp.is_open()) cout << "Invalid file!" << endl;
else {
Student* NewStud = CreateStudent(fp);
AddStudent(FirstStud, NewStud);
}
fp.close();
break;
case REMOVE:
getInputStream(fp);
if (!fp.is_open()) cout << "Invalid file!" << endl;
else RemoveStudent(FirstStud, fp);
fp.close();
break;
case PRINTLIST:
PrintList(FirstStud);
break;
case CLASSLIST:
getInputStream(fp);
if (!fp.is_open()) cout << "Invalid file!" << endl;
else ClassList(FirstStud, fp);
fp.close();
break;
case DEANSLIST:
DeansList(FirstStud);
break;
case QUIT:
cout << "Thank you!" << endl;
return 0;
}
}
return 0;
}