版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、1. 链表的删除如创建一个学生学号及姓名的单链表,即节点包括学生学号、姓名及指向下一个节点的指针,链表按学生的学号排列。再从键盘输入某一学生姓名,将其从链表中删除。首先定义链表的结构:从链表中删除一个节点有三种情况,即删除链表头节点、删除链表的中间节点、删除链表的尾节点。题目给出的是学生姓名,则应在链表中从头到尾依此查找各节点,并与各节点的学生姓名比较,若相同,则查找成功,否则,找不到节点。由于删除的节点可能在链表的头,会对链表的头指针造成丢失,所以定义删除节点的函数的返回值定义为返回结构体类型的指针。cppview plaincopy1. structnode*delet(head,pstr
2、)/*以head为头指针,删除pstr所在节点*/2. structnode*head;3. char*pstr;4. 5. structnode*temp,*p;6. temp=head;/*链表的头指针*/7. if(head=NULL)/*链表为空*/8. printf(nListisnull!n);9. else/*非空表*/10. 11. temp=head;12. while(strcmp(temp-str,pstr)!=0&temp-next!=NULL)/*若节点的字符串与输入字符串不同,并且未到链表尾*/13. 14. p=temp;15. temp=tmep-next;/*
3、跟踪链表的增长,即指针后移*/16. 17. if(strcmp(temp-str,pstr)=0)/*找到字符串*/18. 19. if(temp=head)/*表头节点*/20. printf(deletestring:%sn,temp-str);21. head=head-next;22. free(temp);/*释放被删节点*/23. 24. else25. 26. p-next=temp-next;/*表中节点*/27. printf(deletestring:%sn,temp-str);28. free(temp);29. 30. 31. else32. printf(nnofi
4、ndstring!n);/*没找到要删除的字符串*/33. 34. return(head);35. 2. 链表的插入首先定义链表的结构:cppview plaincopy1. struct2. 3. intnum;/*学生学号*/4. charstr20;/*姓名*/5. structnode*next;6. ;在建立的单链表中,插入节点有三种情况,如图所示;插入的节点可以在表头、表中或表尾。假定我们按照以学号为顺序建立链表,则插入的节点依次与表中节点相比较,找到插入位置。由于插入的节点可能在链表的头,会对链表的头指针造成修改,所以定义插入节点的函数的返回值定义为返回结构体类型的指针。节点的
5、插入函数如下:cppview plaincopy1. structnode*insert(head,pstr,n)/*插入学号为n、姓名为pstr的节点*/2. structnode*head;/*链表的头指针*/3. char*pstr;4. intn;5. 6. structnode*p1,*p2,*p3;7. p1=(structnode*)malloc(sizeof(structnode)/*分配一个新节点*/8. strcpy(p1-str,pstr);/*写入节点的姓名字串*/9. p1-num=n;/*学号*/10. p2=head;11. if(head=NULL)/*空表*/
6、12. 13. head=p1;14. p1-next=NULL;/*新节点插入表头*/15. 16. else/*非空表*/17. 18. while(np2-num&p2-next!=NULL)19. /*输入的学号小于节点的学号,并且未到表尾*/20. 21. p3=p2;22. p2=p2-next;/*跟踪链表增长*/23. 24. if(nnum)/*找到插入位置*/25. if(head=p2)/*插入位置在表头*/26. 27. head=p1;28. p1-next=p2;29. 30. else31. /*插入位置在表中*/32. p3-next=p1;33. p1-nex
7、t=p2;34. 35. else36. /*插入位置在表尾*/37. p2-next=p1;38. p1-next=NULL;39. 40. 41. return(head);/*返回链表的头指针*/42. 实例:创建包含学号、姓名节点的单链表。其节点数任意个,表以学号为序,低学号的在前,高学号的在后,以输入姓名为空作结束。在此链表中,要求删除一个给定姓名的节点,并插入一个给定学号和姓名的节点。cppview plaincopy1. #include2. #include3. structnode/*节点的数据结构*/4. 5. intnum;6. charstr20;7. structno
8、de*next;8. ;9. /*/10. main()11. 12. /*函数声明*/13. structnode*creat();14. structnode*insert();15. structnode*delet();16. voidprint();17. structnode*head;18. charstr20;19. intn;20. head=NULL;/*做空表*/21. head=creat(head);/*调用函数创建以head为头的链表*/22. print(head);/*调用函数输出节点*/23. printf(ninputinsertednum,name:n);
9、24. gets(str);/*输入学号*/25. n=atoi(str);26. gets(str);/*输入姓名*/27. head=insert(head,str,n);/*节点插入链表*/28. 29. print(head);30. printf(ninputdeletedname:n);31. gets(str);/*输入被删姓名*/32. head=delet(head,str);/*用函数删除节点*/33. print(head);/*调用函数输出节点*/34. return;35. 36. /*/37. /*创建链表*/38. structnode*creat(structn
10、ode*head)39. 40. chartemp30;41. structnode*pl,*p2;42. pl=p2=(structnode*)malloc(sizeof(structnode);43. printf(inputnum,name:n);44. printf(exit:doubletimesEnter!n);45. gets(temp);46. gets(pl-str);47. pl-num=atoi(temp);48. pl-next=NULL;49. while(strlen(pl-str)0)50. 51. if(head=NULL)/ifthechainisnull52
11、. head=pl;/53. else54. p2-next=pl;/setthep2-nextNULL55. p2=pl;/56. pl=(structnode*)malloc(sizeof(structnode);57. printf(inputnum,name:n);58. printf(exit:doubletimesEnter!n);59. gets(temp);60. gets(pl-str);61. pl-num=atoi(temp);62. pl-next=NULL;63. 64. returnhead;65. 66. /*/67. /*插入节点*/68. structnode
12、*insert(head,pstr,n)69. structnode*head;70. char*pstr;71. intn;72. 73. structnode*pl,*p2,*p3;74. pl=(structnode*)malloc(sizeof(structnode);75. strcpy(pl-str,pstr);76. pl-num=n;77. p2=head;78. if(head=NULL)79. 80. 81. head=pl;82. pl-next=NULL;83. 84. else85. 86. while(np2-num&p2-next!=NULL)87. 88. p3
13、=p2;89. p2=p2-next;90. 91. if(nnum)92. if(head=p2)93. 94. head=pl;95. pl-next=p2;96. 97. else98. 99. p3-next=pl;100. pl-next=p2;101. 102. else103. 104. p2-next=pl;105. pl-next=NULL;106. 107. 108. return(head);109. 110. /*/111. 112. 113. /*删除节点*/114. structnode*delet(head,pstr)115. structnode*head;11
14、6. char*pstr;117. 118. structnode*temp,*p;119. temp=head;120. if(head=NULL)121. printf(nListisnull!n);122. else123. 124. temp=head;125. while(strcmp(temp-str,pstr)!=0&temp-next!=NULL)126. 127. p=temp;128. temp=temp-next;129. 130. if(strcmp(temp-str,pstr)=0)131. 132. if(temp=head)133. 134. head=head-
15、next;135. free(temp);136. 137. else138. 139. p-next=temp-next;140. printf(deletestring:%sn,temp-str);141. free(temp);142. 143. 144. else145. printf(nnofindstring!n);146. 147. return(head);148. 149. /*/150. /*链表各节点的输出*/151. voidprint(structnode*head)152. 153. structnode*temp;154. temp=head;155. print
16、f(noutputstrings:n);156. while(temp!=NULL)157. 158. printf(n%d-%sn,temp-num,temp-str);159. temp=temp-next;160. 161. return;162. 运行结果:cppview plaincopy1. rootandroid-virtual-machine:/uniteq_smb/test#./test12. inputnum,name:3. exit:doubletimesEnter!4. 115. aa6. inputnum,name:7. exit:doubletimesEnter!8. 129. bb10. inputnum,name:11. exit:d
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年装饰涂料行业分析报告及未来发展趋势报告
- 2026年消渴降糖胶囊行业分析报告及未来发展趋势报告
- 2026年高铝球行业分析报告及未来发展趋势报告
- 2026年投影机行业分析报告及未来发展趋势报告
- 2026年稀土钴行业分析报告及未来发展趋势报告
- 2026贵州长江汽车有限公司招聘20人笔试备考题库及答案解析
- 2026江苏镇江市丹徒区招聘“两新”组织专职党务工作者7人考试备考题库及答案解析
- 2026年PC耐力板行业分析报告及未来发展趋势报告
- 2025年精神卫生(副高)高级职称考试题库及答案
- 2026年热能行业分析报告及未来发展趋势报告
- GB/T 47417-2026蜂蜜中水不溶物的测定
- 泰山教育联盟2026届高三年级4月考试模拟 政治试题(含答案)
- 2026年成都市新都区街道办人员招聘笔试模拟试题及答案解析
- JJG 721-2010相位噪声测量系统
- 第七讲马克思主义与社会科学方法论
- GB/T 15390-2005工程用焊接结构弯板链、附件和链轮
- 人教高中数学必修二B版-《统计》统计与概率课件
- 迈瑞天地人血培养基础介绍
- 煤矿爆破工培训课件
- 北京市药品零售企业药店药房名单目录
- 2022年江苏省苏豪控股集团有限公司招聘笔试题库及答案解析
评论
0/150
提交评论