#include using namespace std; struct employee{//定义职工结构体 int id;//职工编号 int age;//职工年龄 char sex;//职工性别 employee *next;//指向下一个结点的指针 }; int countMale(employee *head);//统计男性职工人数 int countFemale(employee *head);//统计女性职工人数 void addEmployee(employee *&head);//在链表尾部插入新职工结点 void deleteEmployee(employee &head, int id);//删除指定编号的职工结点 employee deleteOld(employee *&head);//删除年龄在60岁以上的男性职工或55岁以上的女性职工结点,并保存在另一个链表中 void printAll(employee *head);//遍历链表输出全部职工信息 int main(){ employee *head = NULL; employee *oldHead = NULL;//存放被删除的老年职工的链表头指针 int choice = 0; int id, age; char sex; while(choice != 6){ cout<<"1.遍历该链表输出全部职工信息"<<endl; cout<<"2.分别统计男、女职工的人数"<<endl; cout<<"3.在链表尾部插入新职工结点"<<endl; cout<<"4.删除指定编号的职工结点"<<endl; cout<<"5.删除年龄在60岁以上的男性职工或55岁以上的女性职工结点,并保存在另一个链表中"<<endl; cout<<"6.退出"<<endl; cout<<"请输入您的选择:"; cin>>choice; switch(choice){ case 1: printAll(head); break; case 2: cout<<"男性职工人数为:"<<countMale(head)<<endl; cout<<"女性职工人数为:"<<countFemale(head)<<endl; break; case 3: addEmployee(head); break; case 4: cout<<"请输入要删除的职工编号:"; cin>>id; deleteEmployee(head, id); break; case 5: oldHead = deleteOld(head); break; case 6: cout<<"谢谢使用!"<<endl; break; default: cout<<"输入有误,请重新输入!"<<endl; break; } } return 0; } void printAll(employee *head){//遍历链表输出全部职工信息 employee *p = head; while(p != NULL){ cout<<"职工编号:"<id<<"\t职工年龄:"<age<<"\t职工性别:"<sex<<endl; p = p->next; } } int countMale(employee *head){//统计男性职工人数 int count = 0; employee *p = head; while(p != NULL){ if(p->sex == 'M'){ count++; } p = p->next; } return count; } int countFemale(employee *head){//统计女性职工人数 int count = 0; employee *p = head; while(p != NULL){ if(p->sex == 'F'){ count++; } p = p->next; } return count; } void addEmployee(employee *&head){//在链表尾部插入新职工结点 int id, age; char sex; employee *p = head; employee *newEmployee = new employee; cout<<"请输入新职工的编号:"; cin>>id; newEmployee->id = id; cout<<"请输入新职工的年龄:"; cin>>age; newEmployee->age = age; cout<<"请输入新职工的性别(M/F):"; cin>>sex; newEmployee->sex = sex; newEmployee->next = NULL; if(head == NULL){//链表为空时 head = newEmployee; }else{//链表不为空时 while(p->next != NULL){ p = p->next; } p->next = newEmployee; } cout<<"新职工添加成功!"<<endl; } void deleteEmployee(employee *&head, int id){//删除指定编号的职工结点 employee *p = head; employee temp; if(head == NULL){//链表为空时 cout<<"链表为空,无法删除!"<<endl; return; } if(head->id == id){//要删除的结点是头结点 head = head->next; delete p; cout<<"职工编号为"<<id<<"的职工已删除!"<<endl; return; } while(p->next != NULL){ if(p->next->id == id){//找到要删除的结点 temp = p->next; p->next = temp->next; delete temp; cout<<"职工编号为"<<id<<"的职工已删除!"<<endl; return; } p = p->next; } cout<<"未找到职工编号为"<<id<<"的职工!"<<endl; } employee deleteOld(employee *&head){//删除年龄在60岁以上的男性职工或55岁以上的女性职工结点,并保存在另一个链表中 employee *p = head; employee *oldHead = NULL;//存放被删除的老年职工的链表头指针 employee *oldTail = NULL;//存放被删除的老年职工的链表尾指针 employee *temp; while(p != NULL){ if((p->sex == 'M' && p->age >= 60) || (p->sex == 'F' && p->age >= 55)){//符合要求时 temp = p; p = p->next; if(temp == head){//要删除的结点是头结点 head = head->next; }else{//要删除的结点不是头结点 oldTail->next = temp->next; } if(oldHead == NULL){//被删除的老年职工的链表为空时 oldHead = temp; oldTail = temp; oldTail->next = NULL; }else{//被删除的老年职工的链表不为空时 oldTail->next = temp; oldTail = temp; oldTail->next = NULL; } }else{//不符合要求时 p = p->next; } } if(oldHead == NULL){//无职工被删除时 cout<<"没有符合要求的职工被删除!"<<endl; }else{//有职工被删除时 cout<<"符合要求的职工已删除,并保存在另一个链表中。"<<endl; } return oldHead; }

使用c++编程;建立一个结点包括职工的编号、年龄和性别的单向链表 分别定义函数完成以下功能 1 遍历该链表输出全部职工信息 2 分别统计男、女职工的人数 3 在链表尾部插入新职工结点 4 删除指定编号的职工结点 5 删除年龄在60岁以上的男性职工或55岁以上的女性职工结点 并保存在另一个链表中 要求 用主函数建立简单菜单选择 并测试程序有中文说明和测试数据

原文地址: https://www.cveoy.top/t/topic/bzfJ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录