建立一个结点包括职工的编号、年龄和性别的单向链表,完成函数功能:遍历,插入,删除等

建立一个结点包括职工的编号、年龄和性别的单向链表,分别定义函数完成以下功能:

(1)遍历该链表输出全部职工信息;

(2)分别统计男、女职工的人数;

(3)在链表尾部插入新职工结点;

(4)删除指定编号的职工结点;

(5)删除年龄在60岁以上的男性职工或55岁以上的女性职工结点,并保存在另一个链表中。

要求:用主函数建立简单菜单选择,并测试程序。

/* 测试数据
    20222222
    57
    woman
    20210102
    20
    man
    20210320
    19
    woman
    20210304
    68
    man
    20109084
    56
    woman
    122223
    18
    woman
    1416346
    17
    man
    */

#include<iostream>
using namespace std;
struct message   //定义结构体
{
	int  num;
	int  year;
	char sex[6];
	message * next;
};
//函数声明
void Creat(message *&);
void ShowList(message *);
void Sum(message *&);
void Insert(message *&);
void Delete(message *&);
void Save(message *&);
/* 测试数据
20222222
57
woman
20210102
20
man
20210320
19
woman
20210304
68
man
20109084
56
woman
122223
18
woman
1416346
17
man
*/
int main()
{
	message *head = NULL;
	cout << "输入初始数据:(输入0退出输入)" << endl;
	Creat(head);    //输入初始数据

	while (1)
	{           //显示一个字符形式的简易菜单
		cout << endl;
		cout << "1-------遍历(ShowList)" << endl;
		cout << "2-------统计(Sum)" << endl;
		cout << "3-------插入(Insert)" << endl;
		cout << "4-------删除(Delete)" << endl;
		cout << "5-------另存(Save)" << endl;
		int x;     //定义选项
		cin >> x;
		switch (x)
		{
		case 1:ShowList(head);
			break;
		case 2: Sum(head);
			break;
		case 3: Insert(head);
			break;
		case 4: Delete(head);
			break;
		case 5:Save(head);
			break;
		default: cout << "输入错误,请重新输入!" << endl; break;
		}
	}
	return 0;
}
void Creat(message * & head)//建立链表,输入数据
{
	message *s, *p;
	s = new message;
	p = new message;
	cin >> s->num >> s->year >> s->sex;
	while (s->num != 0)
	{
		if (head == NULL)
			head = s;
		else
			p->next = s;
		p = s;
		s = new message;
		cin >> s->num;
		if (s->num == 0)//输入0退出函数
			return;
		else
			cin >> s->year >> s->sex;
	}

}
void ShowList(message *head)//遍历该链表输出全部职工信息
{
	message *r = new message;
	r = head;
	cout << "now the items of message are:" << endl;
	while (r != (message *)0xCDCDCDCD)
	{
		cout << r->num << "," << r->year << "," << r->sex << endl;
		r = r->next;
	}
	return;
}

void Sum(message * &head)//分别统计男、女职工的人数
{
	int summ = 0, sumw = 0;
	message *q = head;
	while (q != (message *)0xCDCDCDCD)
	{
		if (strcmp(q->sex, "man") == 0)
		{
			summ++;
		}
		if (strcmp(q->sex, "woman") == 0)
		{
			sumw++;
		}
		q = q->next;
	}
	cout << "男职工人数=" << summ << endl;
	cout << "女职工人数=" << sumw << endl;
}
void Insert(message * &head)//在链表尾部插入新职工结点
{
	message *s, *q, *t;
	s = new message;
	q = head;
	t = head->next;
	while (q->next != (message *)0xCDCDCDCD)   //搜索表尾
	{
		q = t;
		t = q->next;
	}
	q->next = s;//插入表尾
	cin >> s->num >> s->year >> s->sex;
}
void Delete(message * &head)   //删除指定编号的职工结点
{
	int k = 0;
	message *s = new message;
	message *t = new message;
	cout << "请输入指定编号:" << endl;
	cin >> k;
	if (head->num == k)     //如果删除的是头结点
	{
		s = head;
		head = head->next;
		delete s;
		s = NULL;
		cout << "已删除" << endl;
		return;
	}
	else
	{   //查找要删除的结点
		for (s = head; s->next;s=s->next)
		{
			if (s->next->num == k)
			{
				t = s->next;
				s->next = t->next;
				delete t;
				t = NULL;
				cout << "已删除" << endl;
				return;
			}
		}
	}
}
void Save(message * &head)
//删除年龄在60岁以上的男性职工或55岁以上的女性职工结点,
//并保存在另一个链表中.
{
	message *newhead = NULL;
	message *s = new message;
	message *t = new message;
	message *p = NULL;
	message *pt = NULL;
	int a, b;
	do
	{   //如果头结点符合条件
		a = 0;
		b = 0;
		if (strcmp(head->sex, "man") == 0)//是男性
		{
			if (head->year > 60)//年龄大于60
			{
				//存入新链表
				s->num = head->num;
				s->year = head->year;
				strcpy_s(s->sex, head->sex);
				if (newhead == NULL)
					newhead = s;
				else
					t->next = s;
				t = s;
				s = new message;
				//删除原链表结点
				{
					p = head;
					head = head->next;
					delete p;
					p = NULL;
				}
				a = 1;
			}

		}
		else if (strcmp(head->sex, "woman") == 0)//是女性
		{
			if (head->year > 55)//年龄大于55
			{
				//存入新链表
				s->num = head->num;
				s->year = head->year;
				strcpy_s(s->sex, head->sex);
				if (newhead == NULL)
					newhead = s;
				else
					t->next = s;
				t = s;
				s = new message;
				//删除原链表中的相应结点						
				{
					p = head;
					head = head->next;
					delete p;
					p = NULL;
				}
				b = 1;
			}
		}
	} while (a + b == 1 && head != (message *)0xCDCDCDCD);//符合条件时循环,不符合退出循环

	//找出符合条件的其他结点
	pt = head;
	for (; pt->next != (message *)0xCDCDCDCD;)
	{
		if (strcmp(pt->next->sex, "man") == 0)//是男性
		{
			if (pt->next->year > 60)//年龄大于60
			{
				//存入新链表
				s->num = head->num;
				s->year = head->year;
				strcpy_s(s->sex, head->sex);
				if (newhead == NULL)
					newhead = s;
				else
					t->next = s;
				t = s;
				s = new message;
				//删除原链表结点
				{
					p = pt->next;
					pt->next = p->next;
					delete p;
					p = NULL;
				}
			}
			else pt = pt->next;
		}
		else if (strcmp(pt->next->sex, "woman") == 0)//是女性
		{
			if (pt->next->year > 55)//年龄大于55
			{
				//存入新链表
				s->num = head->num;
				s->year = head->year;
				strcpy_s(s->sex, head->sex);
				if (newhead == NULL)
					newhead = s;
				else
					t->next = s;
				t = s;
				s = new message;
				//删除原链表中的相应结点						
				{
					p = pt->next;
					pt->next = p->next;
					delete p;
					p = NULL;
				}
			}
			else pt = pt->next;
		}
	}
}

  • 3
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

star marks

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值