0% found this document useful (0 votes)
22 views8 pages

University Management System

Uploaded by

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

University Management System

Uploaded by

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

#include <iostream>

#include <string>
#include <cstdlib>
using namespace std;

class Student {
public:
int rollNumber;
string name;

string course;
int marks;
Student* next;
};

class Teacher {
public:
int id;
string name;
string subject;
Teacher* next;
};

class ExamSchedule {
public:
string subjectCode;
string course;
string subject;
string date;
string time;
ExamSchedule* next;
};

class UniversityManagementSystem {
private:
Student* studentList;
Teacher* teacherList;
ExamSchedule* examScheduleList;

public:
UniversityManagementSystem() {
studentList = nullptr;
teacherList = nullptr;
examScheduleList = nullptr;
}

void insertStudent(int rollNumber, const std::string& name, const std::string&


course, int marks) {
Student* newStudent = new Student;
newStudent->rollNumber = rollNumber;
newStudent->name = name;
newStudent->course = course;
newStudent->marks = marks;
newStudent->next = nullptr;

if (!studentList) {
studentList = newStudent;
} else {
Student* temp = studentList;
while (temp->next) {
temp = temp->next;
}
temp->next = newStudent;
}

cout << "Student " << name << " added successfully.\n";
}

void deleteStudent(int rollNumber) {


if (!studentList) {
std::cout << "No students found.\n";
return;
}
Student* prev = nullptr;
Student* current = studentList;

while (current && current->rollNumber != rollNumber) {


prev = current;
current = current->next;
}

if (!current) {
std::cout << "Student with Roll Number " << rollNumber << " not found.\n";
return;
}

if (!prev) {
// The student to be deleted is the first one
studentList = current->next;
} else {
prev->next = current->next;
}

delete current;
cout << "Student with Roll Number " << rollNumber << " deleted successfully.\
n";
}

void showStudents() {
if (!studentList) {
std::cout << "No students found.\n";
} else {
std::cout << "Student List:\n";
Student* temp = studentList;
cout<<"Roll No."<<" "<<"name"<<" "<<"course"<<" "<<"Marks"<<endl;
while (temp) {
cout << temp->rollNumber << " "<<temp->name <<" "<< temp-
>course<<" "<<temp->marks<<endl; ;

temp = temp->next;
}
}
}

void insertTeacher(int id, const std::string& name, const std::string& subject)


{
Teacher* newTeacher = new Teacher;
newTeacher->id = id;
newTeacher->name = name;
newTeacher->subject = subject;
newTeacher->next = nullptr;

if (!teacherList) {
teacherList = newTeacher;
} else {
Teacher* temp = teacherList;
while (temp->next) {
temp = temp->next;
}
temp->next = newTeacher;
}

cout << "Teacher " << name << " added successfully.\n";
}

void deleteTeacher(int id) {


if (!teacherList) {
cout << "No teachers found.\n";
return;
}

Teacher* prev = nullptr;


Teacher* current = teacherList;

// Search for the teacher with the given ID


while (current && current->id != id) {
prev = current;
current = current->next;
}

if (!current) {
cout << "Teacher with ID " << id << " not found.\n";
return;
}

if (!prev) {
// The teacher to be deleted is the first one
teacherList = current->next;
} else {
prev->next = current->next;
}

delete current;
cout << "Teacher with ID " << id << " deleted successfully.\n";
}

void showTeachers() {
if (!teacherList) {
cout << "No teachers found.\n";
} else {
std::cout << "Teacher List:\n";
Teacher* temp = teacherList;
while (temp) {
std::cout << "ID: " << temp->id << "\n";
std::cout << "Name: " << temp->name << "\n";
std::cout << "Subject: " << temp->subject << "\n\n";
temp = temp->next;
}
}
}

void insertExamSchedule(const std::string& subjectCode, const std::string&


course, const std::string& subject, const std::string& date, const std::string&
time) {
ExamSchedule* newSchedule = new ExamSchedule;
newSchedule->subjectCode = subjectCode;
newSchedule->course = course;
newSchedule->subject = subject;
newSchedule->date = date;
newSchedule->time = time;
newSchedule->next = nullptr;

if (!examScheduleList) {
examScheduleList = newSchedule;
} else {
ExamSchedule* temp = examScheduleList;
while (temp->next) {
temp = temp->next;
}
temp->next = newSchedule;
}

cout << "Exam schedule for " << subject << " added successfully.\n";
}

void deleteExamSchedule(const std::string& subjectCode) {


if (!examScheduleList) {
cout << "No exam schedules found.\n";
return;
}

ExamSchedule* prev = nullptr;


ExamSchedule* current = examScheduleList;

// Search for the exam schedule with the given subject code
while (current && current->subjectCode != subjectCode) {
prev = current;
current = current->next;
}

if (!current) {
cout << "Exam schedule with Subject Code " << subjectCode << " not found.\
n";
return;
}

if (!prev) {
// The exam schedule to be deleted is the first one
examScheduleList = current->next;
} else {
prev->next = current->next;
}

delete current;
cout << "Exam schedule with Subject Code " << subjectCode << " deleted
successfully.\n";
}

void showExamSchedule() {
if (!examScheduleList) {
cout << "No exam schedules found.\n";
} else {
cout << "Exam Schedule List:\n";
ExamSchedule* temp = examScheduleList;
while (temp) {
cout << "Subject Code: " << temp->subjectCode << "\n";
cout << "Course: " << temp->course << "\n";
cout << "Subject: " << temp->subject << "\n";
cout << "Date: " << temp->date << "\n";
cout << "Time: " << temp->time << "\n\n";
temp = temp->next;
}
}
}

void sortStudentsByRollNumber() {
if (!studentList || !studentList->next) {
// No need to sort if there are 0 or 1 students
return;
}

bool swapped;
do {
swapped = false;
Student* prev = nullptr;
Student* current = studentList;

while (current->next) {
Student* nextStudent = current->next;
if (current->rollNumber > nextStudent->rollNumber) {
// Swap current student with the next student
if (!prev) {
// Updating the head of the list
studentList = nextStudent;
} else {
prev->next = nextStudent;
}
current->next = nextStudent->next;
nextStudent->next = current;
prev = nextStudent;
swapped = true;
} else {
prev = current;
current = current->next;
}
}
} while (swapped);

std::cout << "Students sorted by Roll Number.\n";


}

void sortTeachersById() {
if (!teacherList || !teacherList->next) {
// No need to sort if there are 0 or 1 teachers
return;
}

bool swapped;
do {
swapped = false;
Teacher* prev = nullptr;
Teacher* current = teacherList;

while (current->next) {
Teacher* nextTeacher = current->next;
if (current->id > nextTeacher->id) {
// Swap current teacher with the next teacher
if (!prev) {
// Updating the head of the list
teacherList = nextTeacher;
} else {
prev->next = nextTeacher;
}
current->next = nextTeacher->next;
nextTeacher->next = current;
prev = nextTeacher;
swapped = true;
} else {
prev = current;
current = current->next;
}
}
} while (swapped);

std::cout << "Teachers sorted by ID.\n";


}
~UniversityManagementSystem() {
Student* studentPtr = studentList;
while (studentPtr) {
Student* temp = studentPtr;
studentPtr = studentPtr->next;
delete temp;
}

// Delete the linked list of teachers


Teacher* teacherPtr = teacherList;
while (teacherPtr) {
Teacher* temp = teacherPtr;
teacherPtr = teacherPtr->next;
delete temp;
}

// Delete the linked list of exam schedules


ExamSchedule* examSchedulePtr = examScheduleList;
while (examSchedulePtr) {
ExamSchedule* temp = examSchedulePtr;
examSchedulePtr = examSchedulePtr->next;
delete temp;
}

}
};
int main() {
UniversityManagementSystem ums;

int choice;
while (true) {
cout << "University Management System\n";
cout << "1. Insert Student\n";
cout << "2. Delete Student\n";
cout << "3. Show Students\n";
cout << "4. Insert Teacher\n";
cout << "5. Delete Teacher\n";
cout << "6. Show Teachers\n";
cout << "7. Insert Exam Schedule\n";
cout << "8. Delete Exam Schedule\n";
cout << "9. Show Exam Schedule\n";
cout << "10. Sort Students\n";
cout << "11. Sort Teachers\n";
cout << "12. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1: {
int rollNumber, marks;
string name, course;
cout << "Enter Roll Number: ";
cin >> rollNumber;
cout << "Enter Name: ";
cin.ignore(); // Clear newline from previous input
getline(std::cin, name);
cout << "Enter Course: ";
getline(std::cin, course);
cout << "Enter Marks: ";
cin >> marks;
ums.insertStudent(rollNumber, name, course, marks);
break;
}
case 2: {
int rollNumber;
cout << "Enter Roll Number to delete: ";
cin >> rollNumber;
ums.deleteStudent(rollNumber);
break;
}
case 3:
ums.showStudents();
break;
case 4: {
int id;
string name, subject;
cout << "Enter ID: ";
cin >> id;
cout << "Enter Name: ";
cin.ignore();
getline(std::cin, name);
cout << "Enter Subject: ";
getline(std::cin, subject);
ums.insertTeacher(id, name, subject);
break;
}
case 5: {
int id;
cout << "Enter ID to delete: ";
cin >> id;
ums.deleteTeacher(id);
break;
}
case 6:
ums.showTeachers();
break;
case 7: {
string subjectCode, course, subject, date, time;
cout << "Enter Subject Code: ";
cin.ignore();
getline(std::cin, subjectCode);
cout << "Enter Course: ";
getline(std::cin, course);
cout << "Enter Subject: ";
getline(std::cin, subject);
cout << "Enter Date: ";
getline(std::cin, date);
cout << "Enter Time: ";
getline(std::cin, time);
ums.insertExamSchedule(subjectCode, course, subject, date, time);
break;
}
case 8: {
string subjectCode;
cout << "Enter Subject Code to delete: ";
cin.ignore();
getline(std::cin, subjectCode);
ums.deleteExamSchedule(subjectCode);
break;
}
case 9:
ums.showExamSchedule();
break;
case 10:
ums.sortStudentsByRollNumber();
break;
case 11:
ums.sortTeachersById();
break;
case 12:
cout << "Exiting...\n";
return 0;
default:
cout << "Invalid choice.\n";
}
}

return 0;
}

You might also like