




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、云南大学物理实验教学中心实验报告课程名称: 计算机软件技术基础实验项目: 线性表(链式存储)及其应用学生姓名:学号: 学院系级 专业成绩指导教师: 实验时间:年 时 分至 时 分实验地点:实验类型:教学(演示 验证 综合 设计)学生科研 课外开放测试其它23一、实验目的:掌握链式存储结构下线性表的建立及基本操作。二、问题:建立一个采用链式存储的线性表,表中元素为学生,每个学生信息包含姓名、学号和成绩三部分,对该表实现:输出、查找、插入、删除功能,并计算出平均成绩和总成绩。三、程序的编写与调试1、原程序:#include#includeusing namespace std;struct nod
2、e int num; char name10; float score; node *next;class linked_list private:node *head;public:linked_list();void prt_linked_list();void ins_num_linked_list(int i,node *b);void ins_name_linked_list(char name,node *b);int del_num_linked_list(node);int del_name_linked_list(node);int sear_num_linked_list(
3、int);void sear_name_linked_list(node);void count_linked_list(); error C2533: linked_list:linked_list : constructors not allowed a return typeerror C2264: linked_list:linked_list : error in function definition or declaration; function not called错误原因:“”后缺少“;”改正:在“”后面加上“;” /*建立链表*/ linked_list:linked_l
4、ist()node *p,*q;p=new node;q=new node;p-num=101;strcpy(p-name,aaa);p-score=98; q-num=104;strcpy(q-name,ddd);q-score=95; head=p;p-next=q;q-next=NULL; return ;/* 输出*/void linked_list:prt_linked_list()node *p;p=head;if(head=NULL)cout空链表!”error C2001: newline in constanterror C2143: syntax error : missi
5、ng ; before elseerror C2181: illegal else without matching iferror C2601: ins_num_linked_list : local function definitions are illegal error C2601: ins_name_linked_list : local function definitions are illegalerror C2601: del_num_linked_list : local function definitions are illegalerror C2601: del_n
6、ame_linked_list : local function definitions are illegalerror C2601: sear_num_linked_list : local function definitions are illegalerror C2601: sear_name_linked_list : local function definitions are illegalerror C2601: count_linked_list : local function definitions are illegalerror C2601: main : loca
7、l function definitions are illegalfatal error C1004: unexpected end of file found错误原因:符号错误,“”为中文引号。改正:把中文引号改为英文引号即可。endl;return; else docoutnum name scorenext;while(p!=NULL);return;/*按学号插入*/void linked_list:ins_num_linked_list(int i,node *b) node *q;if(head=NULL)b-next=NULL;head=b;return;if(head-num
8、=i)b-next=head;head=b;return;q=head;while(q-next!=NULL)&(q-next)-num)!=i)q=q-next;if(q-next=NULL)cout无此元素next=q-next;q-next=b; return;/*按姓名插入*/void linked_list:ins_name_linked_list(char name,node *b)node *q;if(head=NULL)b-next=NULL;head=b;return;if(strcmp(head-name,name)=0)b-next=head;head=b return;
9、error C2143: syntax error : missing ; before return错误原因:return前面缺少“;”改正:在return前面加上“;”。q=head;while(q-next!=NULL)&(strcmp(q-next)-name,name)!=0)q=q-next;if(q-next=NULL)cout无此元素next=q-next;q-next=b; return;/*按学号删除*/int linked_list:del_num_linked_list( node x ) node *p,*q;if(head=NULL)cout空链表!num=x.nu
10、m)p=head-next;delete head;head=p;return 1;q=head;while(q-next!=NULL)&(q-next)-num)!=x.num)q=q-next;if(q-next=0)cout链表中无此元素next;q-next=p-next;delete p;return 1;/*按姓名删除*/int linked_list:del_name_linked_list( node x ) node *p,*q;if(head=NULL)cout空链表!name,)=0)p=head-next;delete head;head=p;return
11、;错误:error C2561: del_name_linked_list : function must return a value错误原因:返回值出错;必须要返回一个值。改正:把“return”改为“return 1”q=head;while(q-next!=NULL)&(strcmp(q-next)-name,)!=0)q=q-next;if(q-next=0)cout链表中无此元素next;q-next=p-next;delete p;return 1;/*按学号查找*/int linked_list:sear_num_linked_list( int i ) node
12、*q; if(head=NULL)cout空链表!next!=NULL)&(q-num)!=i)q=q-next;if(q-num)!=i)cout无此元素endl;return 0;coutnum name scoreendl;warning C4715: linked_list:sear_num_linked_list : not all control paths return a value警告原因:没有控制返回值。改正:在“”前面加上返回语句“return 1;”/*按姓名查找*/void linked_list:sear_name_linked_list(node x ) node
13、 *p; if(head=NULL)cout空链表!next!=NULL)&(strcmp(p-name,)!=0)p=p-next;if(strcmp(p-name,)!=0)cout无此元素endl;return 0error C2562: sear_name_linked_list : void function returning a valu错误原因:返回值出错,不能返回0。改正:删除return后面的0.;coutnum name scoreendl;/*计算*/void linked_list:count_linked_list() float sum=0
14、,ave;int n=0;node *p;p=head;if(p=0)cout空链表score; p=p-nxt;error C2039: nxt : is not a member of node错误原因:字符出错,输入时输入出错。改正:把“nxt”改为“next”。 n+;ave=sum/n;cout总 分=sumendl;cout平均分=aveendl;return ;/*主函数*/int main() int mx;linked_list s1; while(1) cout1.输出 2.插入 3.删除 4.查找 5.计算 0.退出endl; coutmx; switch(mx) cas
15、e 1: s1.prt_linked_list(); break;case 2: node *b=new node; int mx1;cout 1.按学号插入 2.按姓名插入 0.退出endl; coutmx1;switch(mx1) case 1error C2065: i : undeclared identifier错误原因:没有对变量“i”定义。改正:对“i”进行定义,即在case 1后面加上语句“int i;”: couti; coutb-numb-nameb-score ; s1.ins_num_linked_list(i,b); s1.prt_linked_list();brea
16、k;case 2: char name10; coutname;coutb-numb-nameb-score ; s1.ins_name_linked_list(name,b); s1.prt_linked_list();break;case 0: cout退出endl; return 0;break;case 3: int mx2; node x;cout请输入要删除的: 1.按学号删除 2.按姓名删除 0.退出endl;coutmx2;switch(mx2)case 1: coutx.num; s1.del_num_linked_list(x);s1.prt_linked_list();b
17、reak;case 2: coutx。error C2018: unknown character 0xa1error C2018: unknown character 0xa3error C2679: binary : no operator defined which takes a right-hand operand of type struct node (or there is no acceptable conversion)error C2146: syntax error : missing ; before identifier nameerror C2065: name
18、: undeclared identifier错误原因:符号错误。改正:把“。”改为“.”即可。name; s1.del_name_linked_list(x); s1.prt_linked_list();break;case 0: cout退出endl; return 0; break; case 4: int mx3; cout1.按学号查找 2.按姓名查找 0.退出endl;coutmx3; switch(mx3)case 1:int i; couti; s1.sear_num_linked_list(i) ; break;case 2:node x; ; s1.se
19、ar_name_linked_list(x) ; break;case 0: cout退出endl; return 0; break; case 5:s1.count_linked_list();break;case 0: cout程序结束endl; return 0; return 0;2、正确程序:#include#includeusing namespace std;struct node int num; char name10; float score; node *next;class linked_list private:node *head;public:linked_lis
20、t();void prt_linked_list();void ins_num_linked_list(int i,node *b);void ins_name_linked_list(char name,node *b);int del_num_linked_list(node);int del_name_linked_list(node);int sear_num_linked_list(int);void sear_name_linked_list(node);void count_linked_list(); /*建立链表*/ linked_list:linked_list()node
21、 *p,*q;p=new node;q=new node;p-num=101;strcpy(p-name,aaa);p-score=98; q-num=104;strcpy(q-name,ddd);q-score=95; head=p;p-next=q;q-next=NULL; return ;/* 输出*/void linked_list:prt_linked_list()node *p;p=head;if(head=NULL)cout空链表!endl; return; else docoutnum name scorenext;while(p!=NULL);return;/*按学号插入*/
22、void linked_list:ins_num_linked_list(int i,node *b) node *q;if(head=NULL)b-next=NULL;head=b;return;if(head-num=i)b-next=head;head=b;return;q=head;while(q-next!=NULL)&(q-next)-num)!=i)q=q-next;if(q-next=NULL)cout无此元素next=q-next;q-next=b; return;/*按姓名插入*/void linked_list:ins_name_linked_list(char name
23、,node *b)node *q;if(head=NULL)b-next=NULL;head=b;return;if(strcmp(head-name,name)=0)b-next=head;head=b;return;q=head;while(q-next!=NULL)&(strcmp(q-next)-name,name)!=0)q=q-next;if(q-next=NULL)cout无此元素next=q-next;q-next=b; return;/*按学号删除*/int linked_list:del_num_linked_list( node x ) node *p,*q;if(hea
24、d=NULL)cout空链表!num=x.num)p=head-next;delete head;head=p;return 1;q=head;while(q-next!=NULL)&(q-next)-num)!=x.num)q=q-next;if(q-next=0)cout链表中无此元素next;q-next=p-next;delete p;return 1;/*按姓名删除*/int linked_list:del_name_linked_list( node x ) node *p,*q;if(head=NULL)cout空链表!name,)=0)p=head-next;del
25、ete head;head=p;return 1;q=head;while(q-next!=NULL)&(strcmp(q-next)-name,)!=0)q=q-next;if(q-next=0)cout链表中无此元素next;q-next=p-next;delete p;return 1;/*按学号查找*/int linked_list:sear_num_linked_list( int i ) node *q; if(head=NULL)cout空链表!next!=NULL)&(q-num)!=i)q=q-next;if(q-num)!=i)cout无此元素endl;retu
26、rn 0;coutnum name scoreendl;return 1;/*按姓名查找*/void linked_list:sear_name_linked_list(node x ) node *p; if(head=NULL)cout空链表!next!=NULL)&(strcmp(p-name,)!=0)p=p-next;if(strcmp(p-name,)!=0)cout无此元素endl;return;coutnum name scoreendl;/*计算*/void linked_list:count_linked_list() float sum=0,ave
27、;int n=0;node *p;p=head;if(p=0)cout空链表score; p=p-next; n+;ave=sum/n;cout总 分=sumendl;cout平均分=aveendl;return ;/*主函数*/int main() int mx;linked_list s1; while(1) cout1.输出 2.插入 3.删除 4.查找 5.计算 0.退出endl; coutmx; switch(mx) case 1: s1.prt_linked_list(); break;case 2: node *b=new node; int mx1;cout 1.按学号插入 2.按姓名插入 0.退出endl; coutmx1;switch(mx1) case 1: int i; couti; coutb-numb-nameb-score ; s1.ins_num_link
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 智能教育与培训解决方案合作协议
- 保密协议金融方面
- 影视行业制作管理与后期剪辑方案
- Unit8 lesson 6教学设计 - 2024-2025学年冀教版英语七年级上册
- 川教版三上信息技术3.2 添加角色 教学设计
- 全国冀教版信息技术四年级上册新授课 第7课 Internet Explorer下载 教学设计
- 2025年简易网站服务合同5篇
- 19 食物与营养 教学设计-2024-2025学年科学三年级上册苏教版
- 电子支付商户合作协议8篇
- 标准驾校培训合同范本8篇
- 低空经济无人机行业市场趋势与竞争分析
- 信息论与编码理论-全
- 正是橙黄橘绿时读书分享好书推介感悟教学课件
- 舌尖上的美食中国美食文化北京小吃介绍
- 南方全站仪NTS-332R说明书
- 2024年航空职业技能鉴定考试-航空乘务员考试近5年真题附答案
- 2024年成人高等教育学士学位英语水平考试大纲
- 职业技术学院《酒店财务管理》课程标准
- 【苏教版信息科技】三年级下册8.1《认识自主可控》教案
- MIL-STD-202-211-2020美国美军标准
- 《假性动脉瘤》课件
评论
0/150
提交评论