电子科技大学软件技术基础实验报告2
- 格式:doc
- 大小:51.00 KB
- 文档页数:11
电子科技大学通信与信息工程学院标准实验报告
(实验)课程名称软件技术基础实验
电子科技大学教务处制表
电子科技大学
实验报告
一、实验室名称:校公共机房
二、实验项目名称:链表程序设计
三、实验学时:4学时
四、实验原理:
使用VS2010等C语言集成开发环境(IDE),在微型计算机上对程序进行编辑、编译、连接与运行。通过上机练习掌握在链表的建立、插入删除等方法和过程。
五、实验目的:
1.熟练链表的概念和基本操作方法。
2.掌握课程平台使用方法。
六、实验内容:
上机完成链表的一系列操作,并用链表完成课后习题9,编程实验,调试运行程序并完成报告。
七、实验器材(设备、元器件):
硬件要求:普通pc机,1G内存,100G硬盘空间即可。
软件要求:Windows 7,包括C编译器的IDE。
八、实验步骤、实验编程与运行结果:
1.程序文件名为***.cpp,源程序清单如下:
/*基础实验1,链表的建立,插入,删除*/
#include
#include
struct list
{
int 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)//输入为零表示输入结束
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 insert(struct list *h,int i,int x)
{
struct list *p,*t;
int j;
p=h;
j=0;
while(p!=NULL&&j { 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,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&&j { 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) 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的节点个数为:%d\n",numnode1); printf("input new element:"); scanf("\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)); display(head1); } /*实验2,链表中元素的删除,删除失败则插入*/ #include #include struct list { int info;//节点信息 struct list *next; };