使用c++语言编写一个程序,要求如下 建立一个节点包括职工的编号,年龄,和性别的信息的单向链表,分别定义函数完成以下功能:1.遍历该链表输出全部职工信息。2.分别统计男女职工的人数。3.在链表尾部插入新职工结点。4.删除指定编号的职工结点。5.删除年龄在60岁以上的男职工或55岁以上的女职工结点,并保存在另一个链表中,要求用主函数建立简单菜单选择,并测试程序
时间: 2024-03-03 08:47:13 浏览: 90
基于C++实现(控制台)实现链表的插入、删除、打印等操作的函数(线程安全型数据结构)【100012475】
好的,下面是我为您提供的代码,希望能够帮到您。
```cpp
#include <iostream>
using namespace std;
struct Employee {
int id; // 职工编号
int age; // 职工年龄
char gender; // 职工性别
Employee* next; // 指向下一个节点的指针
};
class EmployeeList {
public:
EmployeeList() {
head = NULL;
maleCount = 0;
femaleCount = 0;
}
// 遍历链表并输出所有职工信息
void traverse() {
Employee* p = head;
while (p != NULL) {
cout << "ID: " << p->id << ", Age: " << p->age << ", Gender: " << p->gender << endl;
p = p->next;
}
}
// 统计男女职工的人数
void countGender() {
Employee* p = head;
while (p != NULL) {
if (p->gender == 'M') {
maleCount++;
} else if (p->gender == 'F') {
femaleCount++;
}
p = p->next;
}
cout << "Male count: " << maleCount << ", Female count: " << femaleCount << endl;
}
// 在链表尾部插入新职工结点
void insert(Employee* employee) {
if (head == NULL) {
head = employee;
} else {
Employee* p = head;
while (p->next != NULL) {
p = p->next;
}
p->next = employee;
}
if (employee->gender == 'M') {
maleCount++;
} else if (employee->gender == 'F') {
femaleCount++;
}
}
// 删除指定编号的职工结点
void remove(int id) {
Employee* p = head;
Employee* prev = NULL;
while (p != NULL && p->id != id) {
prev = p;
p = p->next;
}
if (p == NULL) {
cout << "Employee with ID " << id << " not found." << endl;
return;
}
if (prev == NULL) {
head = p->next;
} else {
prev->next = p->next;
}
if (p->gender == 'M') {
maleCount--;
} else if (p->gender == 'F') {
femaleCount--;
}
delete p;
}
// 删除年龄在60岁以上的男职工或55岁以上的女职工结点,并保存在另一个链表中
void removeOldEmployees(EmployeeList* oldEmployeeList) {
Employee* p = head;
Employee* prev = NULL;
while (p != NULL) {
Employee* next = p->next;
if ((p->gender == 'M' && p->age >= 60) || (p->gender == 'F' && p->age >= 55)) {
if (prev == NULL) {
head = next;
} else {
prev->next = next;
}
if (p->gender == 'M') {
maleCount--;
} else if (p->gender == 'F') {
femaleCount--;
}
oldEmployeeList->insert(p);
} else {
prev = p;
}
p = next;
}
}
private:
Employee* head; // 链表头指针
int maleCount; // 男职工人数
int femaleCount; // 女职工人数
};
int main() {
EmployeeList employeeList;
EmployeeList oldEmployeeList;
while (true) {
cout << "1. Traverse employee list" << endl;
cout << "2. Count male and female employees" << endl;
cout << "3. Insert new employee" << endl;
cout << "4. Remove employee" << endl;
cout << "5. Remove old employees" << endl;
cout << "6. Exit" << endl;
int choice;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
employeeList.traverse();
break;
case 2:
employeeList.countGender();
break;
case 3:
Employee* employee = new Employee();
cout << "Enter employee ID: ";
cin >> employee->id;
cout << "Enter employee age: ";
cin >> employee->age;
cout << "Enter employee gender (M/F): ";
cin >> employee->gender;
employee->next = NULL;
employeeList.insert(employee);
break;
case 4:
int id;
cout << "Enter ID of employee to remove: ";
cin >> id;
employeeList.remove(id);
break;
case 5:
employeeList.removeOldEmployees(&oldEmployeeList);
cout << "Old employees removed." << endl;
break;
case 6:
return 0;
default:
cout << "Invalid choice." << endl;
break;
}
}
}
```
注:由于本人时间和精力有限,此代码可能还存在一些不足之处,仅供参考。
阅读全文