173 lines
3.7 KiB
C++
173 lines
3.7 KiB
C++
#include <iostream>
|
||
#include <string>
|
||
|
||
using namespace std;
|
||
|
||
struct Student {
|
||
int id;
|
||
string name;
|
||
int age;
|
||
Student* next;
|
||
};
|
||
|
||
Student* head = nullptr; // Define the head of the linked list
|
||
|
||
Student* createStudent(int id, const string& name, int age) {
|
||
Student* s = new Student;
|
||
s->id = id;
|
||
s->name = name;
|
||
s->age = age;
|
||
s->next = nullptr;
|
||
return s;
|
||
}
|
||
|
||
void addStudent(int id, const string& name, int age) { // Add student to the end
|
||
Student* s = createStudent(id, name, age);
|
||
|
||
if (!head) {
|
||
head = s;
|
||
return;
|
||
}
|
||
|
||
Student* p = head;
|
||
while (p->next) {
|
||
p = p->next;
|
||
}
|
||
p->next = s;
|
||
}
|
||
|
||
Student* findStudent(int id) {
|
||
Student* p = head;
|
||
while (p) {
|
||
if (p->id == id)
|
||
return p;
|
||
p = p->next;
|
||
}
|
||
return nullptr;
|
||
}
|
||
|
||
bool updateStudent(int id, const string& newName, int newAge) {
|
||
Student* s = findStudent(id);
|
||
if (!s)
|
||
return false;
|
||
|
||
s->name = newName;
|
||
s->age = newAge;
|
||
return true;
|
||
}
|
||
|
||
bool deleteStudent(int id) {
|
||
if (!head)
|
||
return false;
|
||
|
||
if (head->id == id) { // If head is to be deleted
|
||
Student* tmp = head;
|
||
head = head->next;
|
||
delete tmp;
|
||
return true;
|
||
}
|
||
|
||
Student* p = head;
|
||
while (p->next && p->next->id != id) {
|
||
p = p->next;
|
||
}
|
||
|
||
if (p->next) {
|
||
Student* tmp = p->next;
|
||
p->next = tmp->next;
|
||
delete tmp;
|
||
return true;
|
||
}
|
||
|
||
return false;
|
||
}
|
||
|
||
void showStudents() {
|
||
if (!head) {
|
||
cout << "No student info.\n";
|
||
return;
|
||
}
|
||
|
||
Student* p = head;
|
||
cout << "Student ID\tName\tAge\n";
|
||
while (p) {
|
||
cout << p->id << "\t" << p->name << "\t" << p->age << endl;
|
||
p = p->next;
|
||
}
|
||
}
|
||
|
||
void menu() {
|
||
cout << "\n===== Student Mgmt System =====\n";
|
||
cout << "1. Add student\n";
|
||
cout << "2. Delete student\n";
|
||
cout << "3. Modify student\n";
|
||
cout << "4. Query student\n";
|
||
cout << "5. Display all students\n";
|
||
cout << "0. Exit\n";
|
||
cout << "Please select<63><74>";
|
||
}
|
||
|
||
int main() {
|
||
int choice;
|
||
|
||
do {
|
||
menu();
|
||
cin >> choice;
|
||
|
||
int id, age;
|
||
string name;
|
||
|
||
switch (choice) {
|
||
case 1:
|
||
cout << "Student ID - Name - Age<67><65>";
|
||
cin >> id >> name >> age;
|
||
addStudent(id, name, age);
|
||
cout << "Added successfully.\n";
|
||
break;
|
||
|
||
case 2:
|
||
cout << "Input student ID to delete<74><65>";
|
||
cin >> id;
|
||
if (deleteStudent(id))
|
||
cout << "Deleted.\n";
|
||
else
|
||
cout << "Student not found.\n";
|
||
break;
|
||
|
||
case 3:
|
||
cout << "Enter new Student ID - Name - Age<67><65>";
|
||
cin >> id >> name >> age;
|
||
if (updateStudent(id, name, age))
|
||
cout << "Modified successfully.\n";
|
||
else
|
||
cout << "Student not found.\n";
|
||
break;
|
||
|
||
case 4: {
|
||
cout << "Enter Student ID: ";
|
||
cin >> id;
|
||
Student* s = findStudent(id);
|
||
if (s)
|
||
cout << "Student ID: " << s->id << " Name: " << s->name << " Age: " << s->age << endl;
|
||
else
|
||
cout << "Student not found.\n";
|
||
break;
|
||
}
|
||
|
||
case 5:
|
||
showStudents();
|
||
break;
|
||
|
||
case 0:
|
||
cout << "Exit.\n";
|
||
break;
|
||
|
||
default:
|
||
cout << "Invalid option.\n";
|
||
}
|
||
|
||
} while (choice != 0);
|
||
|
||
return 0;
|
||
}
|