\\source code
#include <iostream>
#include <string>
using namespace std;
struct Student {
int id;
string name;
int age;
string course;
Student* next;
};
class EnrollmentSystem {
private:
Student* head;
public:
EnrollmentSystem() : head(nullptr) {}
void addStudent(int id, const string& name, int age, const string& course) {
Student* newStudent = new Student{id, name, age, course, head};
head = newStudent;
}
void displayStudents() {
Student* current = head;
while (current != nullptr) {
cout << "\nID: " << current->id << "\nName: " << current->name
<< "\nAge: " << current->age << "\nCourse: " << current->course <<
endl;
current = current->next;
}
}
Student* searchStudentById(int id) {
Student* current = head;
while (current != nullptr) {
if (current->id == id) {
return current;
}
current = current->next;
}
return nullptr;
}
Student* searchStudentByName(const string& name) {
Student* current = head;
while (current != nullptr) {
if (current->name == name) {
return current;
}
current = current->next;
}
return nullptr;
}
void updateStudent(int id, const string& name, int age, const string& course) {
Student* student = searchStudentById(id);
if (student != nullptr) {
student->name = name;
student->age = age;
student->course = course;
} else {
cout << "Student not found!" << endl;
}
}
void deleteStudent(int id) {
Student* current = head;
Student* previous = nullptr;
while (current != nullptr && current->id != id) {
previous = current;
current = current->next;
}
if (current == nullptr) {
cout << "Student not found!" << endl;
return;
}
if (previous == nullptr) {
head = current->next;
} else {
previous->next = current->next;
}
delete current;
cout << "Student deleted successfully." << endl;
}
};
int main() {
EnrollmentSystem system;
int choice, id, age;
string name, course;
while (true) {
cout << "\n**********ENROLLMENT SYSTEM***************\n";
cout << "\n1. Add Student\n2. Display Students\n3. Search Student by ID\n4.
Search Student by Name\n5. Update Student\n6. Delete Student\n7. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter ID: ";
cin >> id;
[Link]();
cout << "Enter Name: ";
getline(cin, name);
cout << "Enter Age: ";
cin >> age;
[Link]();
cout << "Enter Course: ";
getline(cin, course);
[Link](id, name, age, course);
cout << "\nStudent Added Successfully!\n";
break;
case 2:
[Link]();
break;
case 3:
cout << "Enter ID: ";
cin >> id;
if (Student* student = [Link](id)) {
cout << "ID: " << student->id << "\nName: " << student->name
<< "\nAge: " << student->age << "\nCourse: " << student-
>course << endl;
} else {
cout << "Student not found!" << endl;
}
break;
case 4:
[Link]();
cout << "Enter Name: ";
getline(cin, name);
if (Student* student = [Link](name)) {
cout << "ID: " << student->id << "\nName: " << student->name
<< "\nAge: " << student->age << "\nCourse: " << student-
>course << endl;
} else {
cout << "Student not found!" << endl;
}
break;
case 5:
cout << "Enter ID: ";
cin >> id;
[Link]();
cout << "Enter new Name: ";
getline(cin, name);
cout << "Enter new Age: ";
cin >> age;
[Link]();
cout << "Enter new Course: ";
getline(cin, course);
[Link](id, name, age, course);
cout << "\nUpdate Successful!\n";
break;
case 6:
cout << "Enter ID: ";
cin >> id;
[Link](id);
cout << "\nDeletion is sucessful.\n";
break;
case 7:
return 0;
default:
cout << "Invalid choice. Please try again." << endl;
}
}
return 0;
}