0% found this document useful (0 votes)
410 views3 pages

Lab 6 Solution C++

The document defines a classlist class to manage student records with linked lists. The class contains methods to add new records, delete records by ID, display all records, and find the best student with the highest total marks. The main function contains a menu to allow the user to select these record management options and call the corresponding classlist methods.

Uploaded by

GurmanterrSingh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
410 views3 pages

Lab 6 Solution C++

The document defines a classlist class to manage student records with linked lists. The class contains methods to add new records, delete records by ID, display all records, and find the best student with the highest total marks. The main function contains a menu to allow the user to select these record management options and call the corresponding classlist methods.

Uploaded by

GurmanterrSingh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Lab 6

#include<iostream>
using namespace std;
struct classinfo {
int stud_id;
float test, assignment, quiz, exam;
classinfo *next;

classinfo() {
test = 0;
assignment = 0;
quiz = 0;
exam = 0;
};
};

class classlist {
classinfo* head;
public:
classlist() {
this->head = NULL;
}

void newrecord() {
classinfo * newStud = new classinfo();
classinfo * current = head;
int check = 0;
//cout << current << endl;

cout << "Enter student ID : ";


cin >> newStud->stud_id;
cout << "Enter midterm test mark [20m]: ";
cin >> newStud->test;
cout << "Enter assignment mark [20m]: ";
cin >> newStud->assignment;
cout << "Enter quiz mark [10m]: ";
cin >> newStud->quiz;
cout << "Enter final exam mark [50m]: ";
cin >> newStud->exam;
newStud->next = NULL;

while (current != NULL) {


if (newStud->stud_id == current->stud_id) {
check++;
}
current = current->next;
}

if (check > 0) {
cout << "Error : Duplicate Student ID";
}
else {
if (head == NULL) {
head = newStud;
}
else {
classinfo * last = head;
while (last->next != NULL) {
last = last->next;
}
last->next = newStud;
}
}
cout << endl;
}

void deleterecord(int ID) {


classinfo * prev = NULL, *toDelete = head;

if (head == NULL) {
cout << "No record in the list" << endl;
}
else {

while (toDelete->stud_id != ID) {


prev = toDelete;
toDelete = toDelete->next;
}
prev->next = toDelete->next; //NULL also OK for last item
delete toDelete;
cout << "Record for student " << ID << " has been deleted.";
}
}

void displayrecord() {
classinfo * current = head;

if (current == NULL) {
cout << "No record in the list" << endl;
}
else {
while (current != NULL) {
cout << "Student ID : " << current->stud_id << " -> Midterm
: " << current->test << ", Quiz : " << current->quiz << ", Assignment : " << current-
>assignment << ", Final Exam : " << current->exam << endl;
current = current->next;
}
}cout << endl;
}

void beststudent() {
classinfo * current = head;
int bestID, currentID;
float bestMark = 0, currentMark;

while (current != NULL) {


currentMark = current->test + current->assignment + current->quiz
+ current->exam;
currentID = current->stud_id;

if (currentMark >= bestMark) {


bestMark = currentMark;
bestID = current->stud_id;
}
cout << current->stud_id;
current = current->next;
}

cout << "Best student is " << bestID << "with " << bestMark << " marks."
<< endl;
cout << endl;
}

};

void main() {
int selection, id;
classlist A;

do {
cout << ":: STUDENT RECORD PROGRAM ::" << endl;
cout << "1. Add new record" << endl;
cout << "2. Delete record" << endl;
cout << "3. Display record" << endl;
cout << "4. Display best student" << endl;
cout << "5. Quit the program" << endl;
cout << "Enter selection : ";
cin >> selection;

switch (selection) {
case 1:
A.newrecord();
break;
case 2:
cout << "Please enter the student ID to be deleted : ";
cin >> id;
A.deleterecord(id);
break;
case 3:
A.displayrecord();
break;
case 4:
A.beststudent();
break;
case 5:
break;
}
} while (selection != 5);
}

You might also like