版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
《数据结构》实验报告实验序号:3实验项目名称:链式表的操作学号0907022107姓名
王青华专业、班11网络工程实验地点1#316指导教师林仙丽实验时间一、实验目的及要求1.通过实验理解单链表的逻辑结构;2.通过实验掌握单链表的基本操作和具体的函数实现。二、实验设备(环境)及要求微型计算机;windows操作系统;MicrosoftVisualStudio6.0集成开发环境。三、实验内容与步骤1.链式表表示和实现线性表的如下:#include"stdio.h"#include"string.h"#include"stdlib.h"#include"ctype.h"typedefstructnode//定义结点{ chardata[10];//结点的数据域为字符串 structnode*next;//结点的指针域}ListNode;typedefListNode*LinkList;//自定义LinkList单链表类型LinkListCreatListR1();//函数,用尾插入法建立带头结点的单链表ListNode*LocateNode(LinkListhead,char*key);//函数,按值查找结点voidDeleteList(LinkListhead,char*key);//函数,删除指定值的结点voidprintlist(LinkListhead);//函数,打印链表中的所有值voidDeleteAll(LinkListhead);//函数,删除所有结点,释放内存//==========主函数==============voidmain(){char*ch,*num;num=newchar;ch=newchar[10];LinkListhead;head=CreatListR1();//用尾插入法建立单链表,返回头指针printlist(head);//遍历链表输出其值printf("Deletenode(y/n):");//输入"y"或"n"去选择是否删除结点scanf("%s",num);if(strcmp(num,"y")==0||strcmp(num,"Y")==0){printf("PleaseinputDelete_data:"); scanf("%s",ch); //输入要删除的字符串DeleteList(head,ch); printlist(head);}DeleteAll(head);//删除所有结点,释放内存}//==========用尾插入法建立带头结点的单链表===========LinkListCreatListR1(void){char*ch; ch=newchar[10];LinkListhead=(LinkList)malloc(sizeof(ListNode));//生成头结点ListNode*s,*r,*pp;r=head;r->next=NULL;printf("Input#toend");//输入"#"代表输入结束printf("PleaseinputNode_data:");scanf("%s",ch);//输入各结点的字符串while(strcmp(ch,"#")!=0){ s=(ListNode*)malloc(sizeof(ListNode)); strcpy(s->data,ch); r->next=s; r=s; r->next=NULL; printf("Input#toend"); printf("PleaseinputNode_data:"); scanf("%s",ch);}returnhead;//返回头指针}//==========按值查找结点,找到则返回该结点的位置,否则返回NULL==========ListNode*LocateNode(LinkListhead,char*key){ListNode*p=head->next;//从开始结点比较while(strcmp(p->data,key)!=0&&p)//直到p为NULL或p-> data为key止 p=p->next;//扫描下一个结点returnp;//若p=NULL则查找失败,否则p指向找到的值为key的结点}//==========删除带头结点的单链表中的指定结点=======voidDeleteList(LinkListhead,char*key){ListNode*p,*r,*q=head;p=LocateNode(head,key);//按key值查找结点的if(p==NULL){//若没有找到结点,退出 printf("positionerror"); exit(0);}while(q->next!=p)//p为要删除的结点,q为p的前结点 q=q->next;r=q->next;q->next=r->next;free(r);//释放结点}//===========打印链表=======voidprintlist(LinkListhead){ListNode*p=head->next;//从开始结点打印while(p){ printf("%s,",p->data); p=p->next;}printf("\n");}//==========删除所有结点,释放空间===========voidDeleteAll(LinkListhead){ListNode*p=head,*r;while(p->next){ r=p->next; free(p); p=r;}free(p);}改写以上程序,实现功能如下:1.编写一个函数计算值域为x的结点个数。运行结果截图:2.编写一个删除链表中值为x的结点的直接前趋结点的算法,若有多个值为x的结点,则删除第一个x的直接前趋结点。运行结果截图:3.写一个对单循环链表进行逆序遍历(打印每个结点的值)的算法。运行结果截图:4.改写CreatListR1函数,使得链表创建时为非降序排列,在上述链表中插入一个元素,使得链表依然升序;运行结果截图:四、实验结果与数据处理详细记录程序在调试过程中出现的问题及解决方法。记录程序执行的结果(贴图)。五、分析与讨论对上机实践结果进行分析,上机的心得体会。六、教师评语签名:日期:成绩附源程序清单:1.#include"stdio.h"#include"stdlib.h"typedefstructListNode{intdata;structListNode*next;}ListNode,*LinkList;voidInsertListNum(LinkListH){ListNode*p,*q;inti;charch=NULL;p=H;while(ch!='\n'){q=(LinkList)malloc(sizeof(ListNode));scanf("%d",&i);q->data=i;q->next=p->next;p->next=q;q=q->next;ch=getchar();}}intCountListSameNum(LinkListH,intx){ListNode*p;intcount=0;p=H->next;while(p){if(p->data==x)count++;p=p->next;}returncount;}voidPrintList(LinkListH){ListNode*p;p=H->next;while(p){printf("%d",p->data);p=p->next;}printf("\n");}main(){intn,x;LinkListH;H=(LinkList*)malloc(sizeof(ListNode));InsertListNum(H);PrintList(H);printf("EntertheNumberyoucount:\n");scanf("%d",&x);n=CountListSameNum(H,x);printf("n=%d",n);}2.#include"stdio.h"#include"stdlib.h"typedefstructListNode{intdata;structListNode*next;}ListNode,*LinkList;voidInsertListNum(LinkListH){ListNode*p,*q;inti;charch;p=H;printf("Enterthenumbers:\n");while(ch!='\n'){q=(LinkList)malloc(sizeof(ListNode));scanf("%d",&i);q->data=i;q->next=p->next;p->next=q;q=q->next;ch=getchar();}}intCountListSameNum(LinkListH,intx){ListNode*p;intcount=0;p=H->next;while(p){if(p->data==x)count++;p=p->next;}returncount;}voidDeleteListNum(LinkListH,intx){ListNode*p,*r;intn;p=H;n=CountListSameNum(H,x);if(n==0)printf("Nofounding!");else{while(p){if(p->data==x){r=p->next;p->next=r->next;free(r);break;//若将break去点,则可以删除所有的值为x的前驱结点}p=p->next;}while(p->next==NULL){printf("\nThisNO.1,NoHead!\n");break;}}}voidPrintList(LinkListH){ListNode*p;p=H->next;while(p){printf("%d",p->data);p=p->next;}printf("\n");}main(){intn,x1,x2;LinkListH;H=(LinkList*)malloc(sizeof(ListNode));InsertListNum(H);PrintList(H);printf("EntertheNumberyoucount:\n");scanf("%d",&x1);n=CountListSameNum(H,x1);printf("n=%d",n);printf("\n");printf("Enterthenumberyoudelete:\n");scanf("%d",&x2);DeleteListNum(H,x2);PrintList(H);}3.#include"stdio.h"#include"stdlib.h"typedefstructListNode{intdata;structListNode*next;}ListNode,*LinkList;voidInsertListNum(LinkListH){ListNode*p,*q;inti=0;charch=NULL;p=H;printf("Enterthenumbers:\n");while(ch!='\n'){q=(LinkList*)malloc(sizeof(ListNode));scanf("%d",&i);q->data=i;q->next=p->next;p->next=q;p=p->next;ch=getchar();}}voidSortListNum(LinkListH){ListNode*p,*q;p=H->next;H->next=NULL;while(p){q=p;p=p->next;q->next=H->next;H->next=q;}}voidPrintList(LinkListH){ListNode*p;p=H;printf("ThenewLinklistis:\n");p=p->next;while(p){printf("%d",p->data);p=p->next;}printf("\n");}main(){LinkListH;H=(LinkList*)malloc(sizeof(ListNode));InsertListNum(H);PrintList(H);SortListNum(H);PrintList(H);}4.#include"stdio.h"#include"stdlib.h"typedefstructListNode{intdata;structListNode*next;}ListNode,*LinkList;voidInsertListNum(LinkListH){ListNode*p,*q;inti;charch;p=H;printf("Enterthenumbers:\n");while(ch!='\n'){q=(LinkList)malloc(sizeof(ListNode));scanf("%d",&i);q->data=i;q->next=p->next;p->next=q;q=q->next;ch=getchar();}}voidPrintList(LinkListH){ListNode*p;p=H->next;while(p){printf("%d",p->data);p=p->next;}printf("\n");}intCountAllListNum(LinkListH){ListNode*p;inti=0;p=H->next;while(p){p=p->next;i++;}returni;}voidSortListNum(LinkListH){ ListNode*p,*s,*pt; p=H; s=p->next; while(p->next!=NULL){ while(s->next!=NULL){ if(p->next->data>s->next->d
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 沙滩节目活动策划方案(3篇)
- 健身全年活动方案策划(3篇)
- 三八送花策划活动方案(3篇)
- 邹城啤酒活动方案策划(3篇)
- 壅水坝施工方案(3篇)
- 2025年市场调查与分析应用指南
- 水仙组织培养方案
- 团建游戏活动策划方案
- 2025年中职服装设计与工艺(服装设计)试题及答案
- 2025年高职民俗学(民俗研究)试题及答案
- 2025年九年级上学期期末英语试卷及答案(共三套)
- 2025年福建会考政治试卷及答案
- DB31∕T 1450-2023 旅游码头服务基本要求
- 2024-2025学年人教版数学七年级上学期期末考试测试卷
- 南宁陈教练2026年版考试大纲广西专升本与职教高考(财经商贸大类)考试大纲对比分析及备考攻略
- 灭菌物品装载课件
- 2025至2030中国电力设备检测行业项目调研及市场前景预测评估报告
- 2025上半年软考系统架构设计师考试真题及答案
- 政务信息化统一建设项目监理服务方案投标文件(技术方案)
- 2025年苏州市事业单位招聘考试教师招聘体育学科专业知识试卷
- 加油站投诉处理培训课件
评论
0/150
提交评论