实验11链表123349c陈正宁.docx
《实验11链表123349c陈正宁.docx》由会员分享,可在线阅读,更多相关《实验11链表123349c陈正宁.docx(12页珍藏版)》请在冰点文库上搜索。
实验11链表123349c陈正宁
淮海工学院计算机科学系
实验报告书
课程名:
《C语言程序设计》
题目:
实验11单链表的应用
班级:
软嵌151
学号:
2015123349
姓名:
陈正宁
1、实验内容或题目
教材330页第9和第12题。
2、实验目的与要求
(1)进一步掌握指针的概念,会定义和使用指针变量。
(2)掌握单链表中结点的定义方法。
(3)掌握单链表的建立、插入和删除等方法。
3、实验步骤与源程序
实验步骤
(1)、
(2)、
源代码
(1)、
#include
#include
#defineLENsizeof(structstudent)
structstudent
{longnum;
floatscore;
structstudent*next;
};
intn;
intmain()
{structstudent*creat();
structstudent*del(structstudent*,long);
structstudent*insert(structstudent*,structstudent*);
voidprint(structstudent*);
structstudent*head,stu;
longdel_num;
printf("inputrecords:
\n");
head=creat();
print(head);
printf("inputthedeletednumber:
");
scanf("%ld",&del_num);
head=del(head,del_num);
print(head);
printf("inputtheinsertedrecord:
");
scanf("%ld,%f",&stu.num,&stu.score);
head=insert(head,&stu);
print(head);
return0;
}
structstudent*creat()
{structstudent*head;
structstudent*p1,*p2;
n=0;
p1=p2=(structstudent*)malloc(LEN);
scanf("%ld,%f",&p1->num,&p1->score);
head=NULL;
while(p1->num!
=0)
{n=n+1;
if(n==1)head=p1;
elsep2->next=p1;
p2=p1;
p1=(structstudent*)malloc(LEN);
scanf("%ld,%f",&p1->num,&p1->score);
}
p2->next=NULL;
return(head);
}
structstudent*del(structstudent*head,longnum)
{structstudent*p1,*p2;
if(head==NULL)
{printf("\nlistnull!
\n");
return(head);
}
p1=head;
while(num!
=p1->num&&p1->next!
=NULL)
{p2=p1;p1=p1->next;}
if(num==p1->num)
{if(p1==head)head=p1->next;
elsep2->next=p1->next;
printf("delete:
%ld\n",num);
n=n-1;
}
elseprintf("%ldnotbeenfound!
\n",num);
return(head);
}
structstudent*insert(structstudent*head,structstudent*stud)
{structstudent*p0,*p1,*p2;
p1=head;
p0=stud;
if(head==NULL)
{head=p0;p0->next=NULL;}
else
{while((p0->num>p1->num)&&(p1->next!
=NULL))
{p2=p1;
p1=p1->next;
}
if(p0->num<=p1->num)
{if(head==p1)head=p0;
elsep2->next=p0;
p0->next=p1;
}
else
{p1->next=p0;p0->next=NULL;}
}
n=n+1;
return(head);
}
voidprint(structstudent*head)
{structstudent*p;
printf("\nNow,These%drecordsare:
\n",n);
p=head;
if(head!
=NULL)
do
{printf("%ld%5.1f\n",p->num,p->score);
p=p->next;
}while(p!
=NULL);
}
(2)、
#include
#include
#defineLENsizeof(structstudent)
structstudent
{
charnum[6];
charname[8];
charsex[6];
intage;
structstudent*next;
}stu[10];
voidmain()
{
structstudent*p,*pt,*head;
inti,length,iage,flag=1;
intfind=0;/*若找到待删除元素,find=1,否则find=0*/
while(flag==1)
{
printf("请输入链表的个数(<10):
");
scanf("%d",&length);
if(length<10)
flag=0;
}
for(i=0;i{
p=(structstudent*)malloc(LEN);
if(i==0)
head=pt=p;
else
pt->next=p;
pt=p;
printf("请输入学号:
");
scanf("%s",p->num);
printf("请输入姓名:
");
scanf("%s",p->name);
printf("请输入性别:
");
scanf("%s",p->sex);
printf("请输入年龄:
");
scanf("%d",&p->age);
}
p->next=NULL;
p=head;
printf("\n学号姓名性别年龄\n");
while(p!
=NULL)
{
printf("%4s%8s%6s%6d\n",p->num,p->name,p->sex,p->age);
p=p->next;
}
printf("\n");
printf("请输入待删除的年龄:
");
scanf("%d",&iage);
pt=head;
p=pt;
if(pt->age==iage)
{
p=pt->next;
head=pt=p;
find=1;
}
else
pt=pt->next;
while(pt!
=NULL)
{
if(pt->age==iage)
{
p->next=pt->next;
find=1;
}
else
p=pt;
pt=pt->next;
}
if(!
find)
printf("未找到年龄%d.",iage);
p=head;
printf("\n");
printf("\n学号姓名性别年龄\n");
while(p!
=NULL)
{
printf("%4s%8s",p->num,p->name);
printf("%6s%6d\n",p->sex,p->age);
p=p->next;
}
}
4、测试数据与实验结果(可以抓图粘贴)
(1)、
(2)、
5、结果分析与实验体会
(1)链表是一种常见的重要的结构数据。
它是动态地进行存储分配的一种结构。
因为是根据需要开辟内存单元,所以不会造成浪费。
(2)链表有一个“头指针”变量。
“表尾”的地址部分放一个“NULL”(表示“空地址”),链表到此结束。
(3)而建立动态列表是指在程序执行过程中从无到有地建立起一个链表,即一个一个地开辟结点和输入各结点数据,并建立起前后相链的关系。
(4)刚拿到第一题时,觉得无从下手。
然后仔细分析发现其实也不是很难,就是烦,因为这道题分为creat函数,print函数,del函数,insert函数,然后再编写一个主函数,然后只需要在主函数指定需要删除和插入的结点的数据。
(5)关于第二题,建立一个链表还是能做的。
关于要删除结点中所包含的你从键盘输入的年龄,使用了“/*若找到待删除元素,find=1,否则find=0*/”。