【数据结构】单向链表实例

前端之家收集整理的这篇文章主要介绍了【数据结构】单向链表实例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

题目(出自《C++程序设计基础》):
建立一个结点包括职工的编号、年龄和性别的单向链表,分别定义函数完成以下功能

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

2)分别统计出男女性职工的人数;

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

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

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

用主函数建立简单菜单选择,测试你的程序。
不多说,上自己的水代码。仅供研究……

  1. #include<iostream>
  2. using namespace std;
  3. struct node
  4. {
  5. int num;
  6. int age;
  7. char name[15];
  8. int sex;
  9. node *next;
  10. };
  11. node *head1=NULL,*head2=NULL,*s,*tail,*tail1;
  12. void create(node *&);
  13. void scan();
  14. void count();
  15. void insert(node *);
  16. void del(int);
  17. void delet();
  18. int main()
  19. {
  20. int n,num;
  21. char ch;
  22. bool w=1;
  23. while(w)
  24. {
  25. cout<<"想要进行的操作是(1.创建职工链表(以编号0结束) 2.遍历职工链表 3.统计男女职工人数 4.插入新职工资料 5.删除指定编号员工资料 6.删除老龄员工资料):";
  26. cin>>n;
  27. switch(n)
  28. {
  29. case 1:create(head1);break;
  30. case 2:scan();break;
  31. case 3:count();break;
  32. case 4:s=new node;insert(s);break;
  33. case 5:cout<<"请输入要删除员工编号:";cin>>num;del(num);break;
  34. case 6:delet();break;
  35. default:cout<<"输入信息错误!"<<endl;
  36. }
  37. cout<<"是否继续操作?(1/0)";
  38. cin>>w;
  39. }
  40. }
  41. void create(node *&head)
  42. {
  43. node *p,*s;
  44. s=new node;
  45. cout<<"编号 姓名 年龄 性别(male:0/female:1)"<<endl;
  46. cin>>s->num;
  47. while(s->num!=0)
  48. {
  49. if(head==NULL) head=s;
  50. else p->next=s;
  51. cin>>s->name>>s->age>>s->sex;
  52. p=s;
  53. s=new node;
  54. cin>>s->num;
  55. }
  56. tail=p;
  57. tail1=s;
  58. p->next=s;
  59. s->next=NULL;
  60. }
  61. void scan()
  62. {
  63. node *p;
  64. cout<<"编号 姓名 年龄 性别(male:0/female:1)"<<endl;
  65. for(p=head1;p->next!=NULL;p=p->next)
  66. cout<<p->num<<" "<<p->name<<" "<<p->age<<" "<<p->sex<<endl;
  67. }
  68. void count()
  69. {
  70. node *p;
  71. int woman=0,man=0;
  72. for(p=head1;p->next!=NULL;p=p->next)
  73. {
  74. if(p->sex==1) woman++;
  75. else man++;
  76. }
  77. cout<<"男士:"<<man<<"人"<<endl<<"女士:"<<woman<<"人"<<endl;
  78. }
  79. void insert(node *s)
  80. {
  81. cout<<"请输入"<<endl;
  82. cout<<"编号 姓名 年龄 性别(male:0/female:1)"<<endl;
  83. cin>>s->num>>s->name>>s->age>>s->sex;
  84. s->next=tail1;
  85. tail->next=s;
  86. tail=s;
  87. }
  88. void del(int a)
  89. {
  90. node *p,*s;
  91. s=head1;
  92. for(p=head1;p->next!=NULL;p=p->next)
  93. {
  94. if(p->num==a)
  95. if(p==head1)
  96. {
  97. head1=p->next;
  98. delete p;
  99. break;
  100. }
  101. else
  102. if(p!=tail)
  103. {
  104. s->next=p->next;
  105. delete p;
  106. break;
  107. }
  108. else
  109. {
  110. tail=s;
  111. s->next=tail1;
  112. delete p;
  113. break;
  114. }
  115. s=p;
  116. }
  117. }
  118. void delet()
  119. {
  120. node *s1,*p1,*s2;
  121. s1=head1;
  122. s2=head1;
  123. for(p1=head1;p1->next!=NULL;p1=p1->next)
  124. {
  125. if((p1->age>60&&p1->sex==0)||(p1->age>55&&p1->sex==1))
  126. {
  127. if(head2==NULL) head2=p1;
  128. else s2->next=p1;
  129. s2=p1;
  130. if(p1!=head1)
  131. s1->next=p1->next;
  132. else
  133. head1=p1->next;
  134. }
  135. s1=p1;
  136. }
  137. }
难,倒不是很难,只不过,这题写单向链表,个人觉得就是在自虐!!!双向的会更方便操作。但是这题基本包含了单向链表的所有操作,用来练习还是比较经典的。这题是我们作业,哈哈……

猜你在找的数据结构相关文章