电子科技大学软件技术基础实验报告2_第1页
电子科技大学软件技术基础实验报告2_第2页
电子科技大学软件技术基础实验报告2_第3页
电子科技大学软件技术基础实验报告2_第4页
电子科技大学软件技术基础实验报告2_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、电子科技大学 通信与信息工程 学院标 准 实 验 报 告(实验)课程名称 软件技术基础实验 电子科技大学教务处制表电 子 科 技 大 学实 验 报 告一、实验室名称: 校公共机房 二、实验项目名称:链表程序设计三、实验学时:4学时四、实验原理:使用VS2010等C语言集成开发环境(IDE),在微型计算机上对程序进行编辑、编译、连接与运行。通过上机练习掌握在链表的建立、插入删除等方法和过程。五、实验目的:1. 熟练链表的概念和基本操作方法。2. 掌握课程平台使用方法。六、实验内容:上机完成链表的一系列操作,并用链表完成课后习题9,编程实验,调试运行程序并完成报告。七、实验器材(设备、元器件):硬

2、件要求:普通pc机,1G内存,100G硬盘空间即可。 软件要求:Windows 7,包括C编译器的IDE。八、实验步骤、实验编程与运行结果:1. 程序文件名为*.cpp,源程序清单如下:/*基础实验1,链表的建立,插入,删除*/#include<stdio.h> #include<stdlib.h>struct listint info;struct list *next;struct list *Create(int *numnode)/创建一个链表struct list *head,*tail,*cnew;head=NULL;int num;printf("

3、;输入数据(以零结束):");while(1) scanf("%d",&num); if(num=0)/输入为零表示输入结束 break; cnew=(struct list*)malloc(sizeof(struct list); cnew->info=num; cnew->next=NULL; if(head=NULL)/若为空则将头节点指向新节点 head=cnew; else tail->next=cnew;/将当前节点的next指向新的节点 tail=cnew; (*numnode)+;return head;void inse

4、rt(struct list *h,int i,int x) struct list *p,*t; int j; p=h; j=0; while(p!=NULL&&j<i-1) p=p->next; j+; if(j!=i-1) printf("overflow!"); else t=(struct list*)malloc(sizeof(struct list); t->info=x; t->next=p->next; p->next=t; struct list *insert_head(struct list *h,

5、int x) struct list *p,*t; p=h; t=(struct list*)malloc(sizeof(struct list); t->info=x; t->next=h; return t;struct list *Delete_head(struct list *h) struct list *p; p = h;h = h->next;free(p); return h;void Delete(struct list *h,int i) struct list *p,*s; int j; p=h; j=0; while(p->next!=NULL

6、&&j<i-1) p=p->next; j=j+1; if(j!=i-1) printf("overflow!");else s=p->next; p->next=p->next->next; free(s);void display(struct list *head)struct list *p;if(head=NULL)printf("链表为空,没有数据n");return;printf("n链表的数据元素:n");for(p=head;p!=NULL;p=p->next

7、) printf("%d ",p->info);printf("n");int main()struct list *head1;int numnode1,point1,point2;int ne,p,q;numnode1=0;head1=NULL;/初始化将节点个数初始化为零head1=Create(&numnode1);display(head1);printf("n链表head1的节点个数为:%dn",numnode1);printf("input new element:");scanf(&q

8、uot;n%d",&ne);printf("nthe point of new element:");scanf("%d",&p);if(p=1) head1=insert_head(head1,ne);else insert(head1,(p-1),ne);display(head1);printf("ndelete point:");scanf("%d",&q);if(q=1) head1=Delete_head(head1);else Delete(head1,(q-1);d

9、isplay(head1);/*实验2,链表中元素的删除,删除失败则插入*/#include<stdio.h>#include<stdlib.h>struct listint info;/节点信息struct list *next;struct list *Create(int *numnode)/创建一个链表struct list *head,*tail,*cnew;head=NULL;int num;printf("输入数据(以零结束):");while(1) scanf("%d",&num); if(num=0)/输

10、入为零表示输入结束 break; cnew=(struct list*)malloc(sizeof(struct list); cnew->info=num; cnew->next=NULL; if(head=NULL)/若为空则将头节点指向新节点 head=cnew; else tail->next=cnew;/将当前节点的next指向新的节点 tail=cnew; (*numnode)+;return head;int find(struct list *head,int fab) struct list *p; int flag=0,j=0; for(p=head;p!

11、=NULL;p=p->next) j=j+1; if(p->info=fab) return j;flag=1; if(flag=0) return -1;void Delete(struct list *h,int i) struct list *p,*s; int j; p=h; j=0; while(p->next!=NULL&&j<i-1) p=p->next; j=j+1; if(j!=i-1) printf("overflow!"); else s=p->next; p->next=p->next-

12、>next; free(s); void insert(struct list *h,int i,int x) struct list *p,*t; int j; p=h; j=0; while(p!=NULL&&j<i-1) p=p->next; j+; if(j!=i-1) printf("overflow!"); t=(struct list*)malloc(sizeof(struct list); t->info=x; t->next=p->next; p->next=t;void display(struc

13、t list *head)/遍历链表输出struct list *p;if(head=NULL)printf("链表为空,没有数据n");return;printf("n-链表的数据元素-n");for(p=head;p!=NULL;p=p->next) printf("%d ",p->info);printf("n");int main()struct list *head1;int numnode1,a,q;numnode1=0;head1=NULL;/初始化将节点个数初始化为零head1=Crea

14、te(&numnode1);display(head1);printf("delete:");scanf("%d",&a);q=(find(head1,a)-1);if(q<0) insert(head1,numnode1,a); else Delete(head1,q); display(head1);/*实验3,课本习题,自动判断位置并插入,链表实现*/#include<stdio.h>#include<stdlib.h>struct listint info;struct list *next;stru

15、ct list *Create(int *numnode)/创建一个链表struct list *head,*tail,*cnew;head=NULL;int num;printf("输入数据(以零结束):");while(1) scanf("%d",&num); if(num=0)/输入为零表示输入结束 break; cnew=(struct list*)malloc(sizeof(struct list); cnew->info=num; cnew->next=NULL; if(head=NULL)/若为空则将头节点指向新节点 h

16、ead=cnew; else tail->next=cnew;/将当前节点的next指向新的节点 tail=cnew; (*numnode)+;return head;void insert(struct list *h,int i,int x) struct list *p,*t; int j; p=h; j=0; while(p!=NULL&&j<i-1) p=p->next; j+; if(j!=i-1) printf("overflow!"); else t=(struct list*)malloc(sizeof(struct li

17、st); t->info=x; t->next=p->next; p->next=t; struct list *insert_head(struct list *h,int x) struct list *p,*t; p=h; t=(struct list*)malloc(sizeof(struct list); t->info=x; t->next=h; return t;int find_position(struct list *head,int n_ew) struct list *p; int flag,j=0; for(p=head;p!=NU

18、LL;p=p->next) j=j+1; if(n_ew<=p->info) return j;flag=1;break; void display(struct list *head)struct list *p;if(head=NULL)printf("链表为空,没有数据n");return;printf("n链表的数据元素:n");for(p=head;p!=NULL;p=p->next) printf("%d ",p->info);printf("n");int main()struct list *head1;int numnode1,point1,point2;int ne,p;numnode1=0;head

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论