使用c++代码实现建立一个结点包括职工的编号、年龄和性别的单向链表 分别定义函数完成以下功能 1 遍历该链表输出全部职工信息 2 分别统计男、女职工的人数 3 在链表尾部插入新职工结点 4 删除指定编号的职工结点 5 删除年龄在60岁以上的男性职工或55岁以上的女性职工结点 并保存在另一个链表中 要求 用主函数建立简单菜单选择 并测试程序
#include
struct Employee { int id; int age; char gender; Employee* next; };
Employee* createEmployee(int id, int age, char gender) { Employee* employee = new Employee; employee->id = id; employee->age = age; employee->gender = gender; employee->next = NULL; return employee; }
void printAllEmployees(Employee* head) { Employee* current = head; while (current != NULL) { cout << "ID: " << current->id << ", Age: " << current->age << ", Gender: " << current->gender << endl; current = current->next; } }
void countEmployeesByGender(Employee* head, int& maleCount, int& femaleCount) { Employee* current = head; while (current != NULL) { if (current->gender == 'M') { maleCount++; } else if (current->gender == 'F') { femaleCount++; } current = current->next; } }
void insertEmployee(Employee*& head, Employee* employee) { if (head == NULL) { head = employee; } else { Employee* current = head; while (current->next != NULL) { current = current->next; } current->next = employee; } }
void deleteEmployeeById(Employee*& head, int id) { if (head == NULL) { return; } if (head->id == id) { Employee* temp = head; head = head->next; delete temp; return; } Employee* current = head; while (current->next != NULL) { if (current->next->id == id) { Employee* temp = current->next; current->next = temp->next; delete temp; return; } current = current->next; } }
void deleteOldEmployees(Employee*& head, Employee*& oldEmployeesHead) { if (head == NULL) { return; } if ((head->age >= 60 && head->gender == 'M') || (head->age >= 55 && head->gender == 'F')) { Employee* temp = head; head = head->next; temp->next = oldEmployeesHead; oldEmployeesHead = temp; deleteOldEmployees(head, oldEmployeesHead); } else { deleteOldEmployees(head->next, oldEmployeesHead); } }
int main() { Employee* head = NULL; Employee* oldEmployeesHead = NULL; int maleCount = 0; int femaleCount = 0; int choice; do { cout << "1. Print all employees" << endl; cout << "2. Count employees by gender" << endl; cout << "3. Insert a new employee" << endl; cout << "4. Delete an employee by ID" << endl; cout << "5. Delete old employees" << endl; cout << "6. Exit" << endl; cout << "Enter your choice: "; cin >> choice; switch (choice) { case 1: printAllEmployees(head); break; case 2: maleCount = 0; femaleCount = 0; countEmployeesByGender(head, maleCount, femaleCount); cout << "Male count: " << maleCount << endl; cout << "Female count: " << femaleCount << endl; break; case 3: int id, age; char gender; cout << "Enter ID: "; cin >> id; cout << "Enter age: "; cin >> age; cout << "Enter gender (M/F): "; cin >> gender; insertEmployee(head, createEmployee(id, age, gender)); break; case 4: int deleteId; cout << "Enter ID to delete: "; cin >> deleteId; deleteEmployeeById(head, deleteId); break; case 5: deleteOldEmployees(head, oldEmployeesHead); cout << "Old employees: " << endl; printAllEmployees(oldEmployeesHead); break; case 6: cout << "Exiting program..." << endl; break; default: cout << "Invalid choice" << endl; break; } } while (choice != 6); return 0; }
原文地址: https://www.cveoy.top/t/topic/br6i 著作权归作者所有。请勿转载和采集!