From 61a4aa2dc2fbf50009d6e6d6e2b2c144e9368d0e Mon Sep 17 00:00:00 2001 From: NanamiAdmin Date: Thu, 15 Jan 2026 01:24:10 +0800 Subject: [PATCH] Upload files to "/" --- linkedList.cpp | 172 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 linkedList.cpp diff --git a/linkedList.cpp b/linkedList.cpp new file mode 100644 index 0000000..cf086a3 --- /dev/null +++ b/linkedList.cpp @@ -0,0 +1,172 @@ +#include +#include + +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£º"; +} + +int main() { + int choice; + + do { + menu(); + cin >> choice; + + int id, age; + string name; + + switch (choice) { + case 1: + cout << "Student ID - Name - Age£º"; + cin >> id >> name >> age; + addStudent(id, name, age); + cout << "Added successfully.\n"; + break; + + case 2: + cout << "Input student ID to delete£º"; + cin >> id; + if (deleteStudent(id)) + cout << "Deleted.\n"; + else + cout << "Student not found.\n"; + break; + + case 3: + cout << "Enter new Student ID - Name - Age£º"; + 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; +}