




已阅读5页,还剩11页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实验11 指针进阶实验3 单项链表【实验目的】1掌握单向链表的概念和建立方法。2掌握单向链表的基本操作。【实验内容】一调试示例输入若干个学生的信息(学号、姓名、成绩),当输入学号为0时结束,用单向链表组织这些学生信息后,再按顺序输出。(源程序error11_3.cpp)源程序(有错误的程序)1#include2#include3#include4struct stud_node5int num;6char name20;7int score;8struct stud_node *next;9;10int main(void)1112struct stud_node *head,*tail, *p;13int num, score;14char name20;15int size = sizeof(struct stud_node);16head = tail = NULL;17printf(Input num,name and score:n);18scanf(%d, &num);19/*建立单向链表*/20while(num != 0)21p = malloc(size);22scanf(%s%d, name, &score);23p-num = num;24strcpy(p-name, name);25p-score = score; 26p-next = NULL;27tail-next = p; 28tail = p;29scanf(%d, &num);3031/*输出单向链表*/32for(p = head; p-next != NULL; p = p-next) /*调试时设置断点*/33printf(%d %s %dn, p-num, p-name, p-score);34return 0;35运行结果(改正后程序的运行结果):Input num,name and score:1 zhang 782 wang 803 Li 754 zhao 8501 zhang 782 wang 803 Li 754 zhao 851编译后共有 1 errors,鼠标双击第一个错误,观察源程序中箭头指向第 21 行,分析错误原因并改正:错误信息: : error C2440: = : cannot convert from void * to struct stud_node * ;错误原因: 动态申请要指定其类型 ;改正方法: 在malloc前加上 (struct stud_node*) ;2改正错误后重新编译、连接程序没有错误。运行程序,当输入完第一行数据按回车后,出现出错信息。错误信息: ;错误原因:在建立链表时没有考虑初始状态链表是空的情况;改正方法:在第27行前插入语句: if(head=NULL)head=p; tail = p;else ;3改正错误后重新编译、运行程序,运行结果与预期不符之处为: 少了一行 ;4调试步骤:(1)设置断点,具体位置见源程序的注释。(2)单击(go)按钮,输入题目中给出的运行数据,程序运行到断点,在观察窗口中输入head,单击head前面的加号,就可以看到链表第一个结点的内容,再单击next前面的加号,就可以看到链表第二个结点的内容,以此类推(如图1所示)。经查看,各元素值与输入的数据一致,说明链表建立正确。(3)继续单步运行,同时观察输出窗口的信息。发现当输出倒数第二个学生信息后for循环就结束了。错误原因: 最后一个学生的信息输出不了 ;改正方法: 32改 for(p = head; p != NULL; p = p-next) ;(4)单击(Stop Debugging )按钮,结束程序调试。图1 链表调试5重新编译、连接、运行,运行结果与预期相符。思考:(1) 为什么要用动态内存分配方式建立链表结点? 省空间 运算效率高 (2) 在链表结构中,next成员起什么作用? 记录下一个结构体的位置 二改错题输入若干个学生的学号(共7位,其中第2、3位是专业编号),以#作为输入结束标志,将其生成一个链表,统计链表中专业为计算机(编号为02)的学生人数。(源程序error11_4.cpp)输入输出示例102120220223108102134103091231102034021205#3源程序(有错误的程序)1#include2#include3#include4struct node5char code8;6struct node *next;7;8int main(void)910struct node *head, *p;11int i, n, count;12char str8;13int size = sizeof(struct node);14head = NULL;15gets(str);16/* 按输入数据的逆序建立链表 */17while(strcmp(str, ”#”) != 0)18p = (struct node *)malloc(size);19strcpy(p-code, str);20head = p-next;21head = p; 22gets(str);2324count = 0;25for(p = head; p-next != NULL; p = p-next)26if(p-(code1) = 0 & p-(code2) = 2)27count+;28printf(“%dn”, count);29return 0;301编译后共有 3 errors,鼠标双击第一个错误,观察源程序中箭头位置,分析错误原因并改正:错误信息: : error C2059: syntax error : ( ;错误原因: 括号不用加 ;改正方法: 把26行中的 code前后的括号去掉 ;2改正上述错误后,再次编译连接后无错误出现。运行程序,输入测试数据。运行出错情况: 运行不了 对程序中的数据输入及链表建立过程进行单步调试,以查找错误。查错过程: 错误原因: 没有建立链表 ;改正方法: 删去20 21 加入 p-next =NULL;if(head=NULL)head=p;t=p;elset-next =p;t=p; t=p; 要在上面定义一个struct node *t ;3改正上述错误后,链表能够正确建立。再次运行程序。运行结果: 没有 是否正确: 错误 对程序中的for循环部分进行单步调试,以查找错误。查错过程: 错误原因: for(p = head; p-next != NULL; p = p-next) 错了 ;改正方法: 改为 for(p = head; p!= NULL; p = p-next) ;4改正上述错误后,再次运行程序,运行结果正确。改错汇总:#include#include#includestruct stud_nodeint num;char name20;int score;struct stud_node *next; int main(void)struct stud_node *head,*tail, *p;int num, score;char name20;int count;int size = sizeof(struct stud_node);head = tail = NULL;printf(Input num,name and score:n);scanf(%d, &num);while(num != 0)p = (struct stud_node*)malloc(size);scanf(%s%d, name, &score);p-num = num;strcpy(p-name, name);p-score = score; p-next = NULL; if(head=NULL)head=p; tail = p;elsetail-next = p; tail = p;scanf(%d, &num);scanf(%d,&count);for(p = head; p!= NULL; p = p-next) if(p-score=count)printf(%d %s %dn, p-num, p-name, p-score);return 0;错误行号: 26 正确语句: if(p-code1 = 0 & p-code2 = 2) 错误行号: 25 正确语句: for(p = head; p!= NULL; p = p-next) 错误行号: 20 21 正确语句: p-next =NULL;if(head=NULL)head=p;t=p;elset-next =p;t=p; 三编程题1输入若干个学生信息(包括学号、姓名和成绩),输入学号为0时输入结束,建立一个单向链表,再输入一个成绩值,将成绩大于等于该值的学生信息输出。输入输出示例1 zhang 782 wang 803 Li 754 zhao 850802 wang 804 zhao 85#include#include#includestruct stud_nodeint num;char name20;int score;struct stud_node *next; int main(void)struct stud_node *head,*tail, *p;int num, score;char name20;int count;int size = sizeof(struct stud_node);head = tail = NULL;printf(Input num,name and score:n);scanf(%d, &num);while(num != 0)p = (struct stud_node*)malloc(size);scanf(%s%d, name, &score);p-num = num;strcpy(p-name, name);p-score = score; p-next = NULL; if(head=NULL)head=p; tail = p;elsetail-next = p; tail = p;scanf(%d, &num);scanf(%d,&count);for(p = head; p!= NULL; p = p-next) if(p-score=count)printf(%d %s %dn, p-num, p-name, p-score);return 0;2输入若干个正整数(输入-1为结束标志),要求按输入数据的逆序建立一个链表,并输出。输入输出示例1 2 3 4 5 6 7 -17 6 5 4 3 2 1#include#include#includestruct stud_nodeint num;struct stud_node *next;struct stud_node *Creat_Stu_Doc();void Ptrint_Stu_Doc(struct stud_node *head);int main()struct stud_node *head,*ptr;head=Creat_Stu_Doc();if(head=NULL)printf(No Recordsn);for(ptr=head;ptr;ptr=ptr-next)printf(%d ,ptr-num);printf(n);struct stud_node *Creat_Stu_Doc()struct stud_node *head,*p;int num;int size=sizeof(struct stud_node);head=NULL;scanf(%d,&num);while(num!=-1)p=(struct stud_node *)malloc(size);p-num=num; p-next=head; head=p;scanf(%d,&num);return head;3输入若干个正整数(输入-1为结束标志),并建立一个单向链表,将其中的偶数值结点删除后输出。输入输出示例1 2 3 4 5 6 7 -11 3 5 7#include#include#includestruct stud_nodeint num;struct stud_node *next;int main(void)struct stud_node *head,*tail,*tempP,*frontP,*deleteP,*p,*t;int num;int count;int size = sizeof(struct stud_node);head = tail = NULL;printf(输入数据n);scanf(%d, &num);while(num!=-1)p = (struct stud_node*)malloc(size);p-num = num;p-next = NULL; if(head=NULL)head=p; tail = p;elsetail-next = p; tail = p;scanf(%d, &num);tempP=head;while(tempP!=NULL)if(0=tempP-num%2)if(tempP=head)head=tempP-next;else/找到偶数的前一个数,该数的next指向偶数的next;for(frontP=head;NULL!=frontP;+frontP) if(frontP-next=tempP)frontP-next=tempP-next;break; deleteP=tempP;tempP=tempP-next;if(deleteP-num%2=0)delete deleteP;tempP=head;while(tempP!=NULL!)printf(%dn,tempP-num);deleteP=tempP;tempP=tempP-next;delete deleteP;4输入若干个正整数(输入-1为结束标志)建立两个已按升序排序的单向链表,头指针分别为list1、list2,把两个链表拼成一个链表,并输出新链表信息。要求自定义函数,实现将两个链表拼成一个链表,并返回拼组后的新链表。输入输出示例1 3 5 7 -12 4 6 -11 2 3 4 5 6 7(下面我参考了网上的答案的)#include#includestruct stud_nodeint num;struct stud_node *next;struct stud_node *Creat_Stu_Doc();void Ptrint_Stu_Doc(struct stud_node *head);struct stud_node *InserDoc(struct stud_node *list1,struct stud_node *list2);void main()struct stud_node *list1,*list2;list1=Creat_Stu_Doc();list2=Creat_Stu_Doc();list1=InserDoc(list1,list2);Ptrint_Stu_Doc(list1);/*/struct stud_node *Creat_Stu_Doc()struct stud_node *head,*tail,*p;int num;int size=sizeof(struct stud_node);head=tail=NULL;scanf(%d,&num);while(num!=-1)p=(struct stud_node *)malloc(size);p-num=num;p-next=NULL;if(head=NULL) head=p;elsetail-next=p;tail=p;scanf(%d,&num);return head;/*/void Ptrint_Stu_Doc(struct stud_node *head)struct stud_node *ptr;if(head=NULL)printf(No Recordsn);return;for(ptr=head;ptr;ptr=ptr-next)printf(%d ,ptr-num);printf(n);/*/struct stud_node *InserDoc(struct stud_node *list1,struct stud_node *list2)struct stud_node *ptr,*ptr1,*ptr2;ptr2=list1;ptr=list2;while(list2!=NULL)if(list1=NULL)list1=ptr;/*list1-next=NULL; ?*/elsewhile(ptr-numptr2-num)&(ptr2-next!=NULL)ptr1=ptr2; ptr2=ptr2-next;if(ptr-numnum)list2=list2-next; if(list1=ptr2)list1=ptr;ptr1=ptr2;elseptr1-next=ptr;ptr-next=ptr2;ptr=list2;ptr1=ptr1-next; else ptr2-next=ptr; break; return list1;5输入若干个正整数(输入-1为结束标志)建立一个单向链表,头指针为L,将链表L中奇数值的结点重新组成一个新的链表NEW,并输出新建链表的信息。输入输出示例1 2 3 4 5 6 7 -11
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 基于声学增强现实-洞察及研究
- 2025年学历类自考专业(护理)-护理学研究参考题库含答案解析(5套)
- 2025年学历类自考专业(建筑工程)混凝土及砌体结构-土木工程制图参考题库含答案解析(5套)
- 2025年学历类自考专业(建筑工程)工程地质及土力学-工程测量参考题库含答案解析(5套)
- 2025年学历类自考专业(建筑工程)-钢结构参考题库含答案解析(5套)
- 2025年学历类自考专业(工商企业管理)管理学原理-企业管理概论参考题库含答案解析(5套)
- 2025年学历类自考专业(工商企业管理)-国际企业管理参考题库含答案解析(5套)
- 2025年学历类自考专业(小学教育)现代教育技术-汉语基础参考题库含答案解析(5套)
- 2025年学历类自考专业(小学教育)小学语文教学论-心理卫生与心理辅导参考题库含答案解析(5套)
- 2025年学历类自考专业(小学教育)小学班主任-汉语基础参考题库含答案解析(5套)
- 2025山西临汾市洪洞县招聘专职社区工作者58人考试备考试题及答案解析
- 2025年事业单位工勤技能-吉林-吉林收银员二级(技师)历年参考题库含答案解析(5套)
- GB/T 46010-2025信息技术矿山大数据技术要求
- 2025年“中央八项规定”精神学习知识竞赛测试题库及答案
- 2025年HACCP食品安全内审员考核试题含答案
- 比音勒芬品牌现状分析及二次增长战略
- 2025至2030年中国学前教育市场供需格局及未来发展趋势报告
- 桡骨茎突腱鞘炎的护理查房
- 2025年财务转正考试题库
- 安徽土增管理办法
- 2025年中国白酒行业发展状况与消费行为调查数据
评论
0/150
提交评论