顺序表链表总结实验报告.docx
- 文档编号:12836909
- 上传时间:2023-04-22
- 格式:DOCX
- 页数:16
- 大小:38.23KB
顺序表链表总结实验报告.docx
《顺序表链表总结实验报告.docx》由会员分享,可在线阅读,更多相关《顺序表链表总结实验报告.docx(16页珍藏版)》请在冰豆网上搜索。
顺序表链表总结实验报告
实验报告
实验目的:
学生管理系统(顺序表)
实验要求:
1.建表
2.求表长
3.插入
4.查找
5.删除
6.列表
7.退出
源程序:
#include
#include
#include
#defineMaxSize1000
typedefstruct
{
charxh[40];
charxm[40];
intcj;
typedefstruct{
DataTypedata[MaxSize];//定义表的数据类型
intlength;//数据元素分别放置在data[0]到data[length-1]
当中
}SqList;//表的结构
voidliebiao(SqList*L)//建立表格
{
intk,n;
charq;
printf("请输入,输入学生的个数:
\n");
fflush(stdin);
scanf("%d",&n);
for(k=0;k<=n-1;k++)
{
printf("请输入学生学号\n");
scanf("%s",L->data[k].xh);
printf("请输入学生名字\n");
scanf("%s",L->data[k].xm);
printf("请输入学生成绩\n");
scanf("%d",&L->data[k].cj);
L->length=n;
}
voidqb(SqList*L)//全部输出
{
intk,w;
for(k=0;k
{
w=k+1;
printf("第%睢学生:
”,w);
printf("%s%s%d\n",L->data[k].xh,L->data[k].xm,L->data[k].cj);
}
插入
}
intcr(SqList*L,DataType*xs,inti)//
信息
intj;
if(L->length==MaxSize)
{
printf("没有!
");
return0;
}
elseif((i<0)||(i>L->length))
{
printf("程序溢出,不符合");
return0;
}
else
{
for(j=L->length-1;j>=i;j--)
{
strcpy(L->data[j+1].xh,L->data[j].xh);
strcpy(L->data[j+1].xm,L->data[j].xm);
L->data[j+1].cj=L->data[j].cj;
}
strcpy(L->data[i].xh,xs->xh);
strcpy(L->data[i].xm,xs->xm);
L->data[i].cj=xs->cj;
L->length=L->length+1;
}
return0;
}
intcz(SqList*L)//
{
查找信息
charxh[40];
charxm[40];
intcj;
inti=0,u;
printf("1
printf("1
printf("1
printf("请选择:
");fflush(stdin);scanf("%d",&u);
if(u==1)
{
printf("请输入要查找学生的学号:
");
scanf("%s",xh);
for(i=0;i
if(strcmp(L->data[i].xh,xh)==0)returni;
}
}
if(u==2)
{
printf("请输入要查找学生的姓名:
");
scanf("%s",xm);
for(i=0;i
{
if(strcmp(L->data[i].xm,xm)==0)
returni;
}
}
if(u==3)
{
printf("请输入要查找学生的成绩:
");
scanf("%s",cj);
for(i=0;i
if(L->data[i].cj,&cj)
returni;
return-1;//*如果没找到,返回-1
}
intcz2(SqList*L)//删除查找的函数
{
charxh[40];
charxm[40];
inti=0,h;
printf("1、按学号删除\n");
printf("2、按姓名删除\n");
printf("请选择:
");
fflush(stdin);
scanf("%d",&h);
if(h==1)
{
printf("请输入要删除学生的学号:
");
scanf("%s",xh);
for(i=0;i
判断输入和
if(strcmp(L->data[i].xh,xh)==0)//
已知学号一样不
returni;
}
}
elseif(h==2)
{
printf("请输入要删除学生的姓名:
");
scanf("%s",xm);
for(i=0;i
{
if(strcmp(L->data[i].xm,xm)==0)//断输入姓名和已知姓名一样不
returni;
}
}
return-1;
}
voidsc(SqList*L)//删除函数
inti,j;
printf("请先选择您要删除的学生信息的方式:
\n");
scanf("%d",&j);
i=cz2(L);
if(i==-1)
{
printf("没有查到要删除的学生信息");
return;
}
要删除学
就是后一
for(j=i;j
{
L->data[j].cj=L->data[j+1].cj;//个覆盖了前一个
strcpy(L->data[j].xh,L->data[j+1].xh);
strcpy(L->data[j].xm,L->data[j+1].xm);
}
L->length--;
printf("该学生信息已被删除!
\n");
}
intbc(SqList*L)
return(L->length);
intmain()//主体大函数
{
inti,k;
SqList*L;//定义顺序表的指针
DataType*xs;
L=(SqList*)malloc(sizeof(SqList)*MaxSize);
charq;
ee:
rewind(stdin);
{
printf("
学生管理系统\n");
//
个结构
printf("
\n");
printf("
\n");
printf("
\n");
printf("
建立表格请输入1
\n");
printf("
求表长请输入2
\n");
printf("
插入请输入3
\n");
printf("
查找请输入4
\n");
printf("
删除请输入5
\n");
printf("
列表请输入6
\n");
函数的各
printf("
请输入");scanf("%c",&q);
}
if(q=='1')
{
rewind(stdin);liebiao(L);
gotoee;
}
if(q=='2')
{
rewind(stdin);
bc(L);
printf("共%小个学生\n",L->length);
gotoee;
}
if(q=='3')
rewind(stdin);
printf("插入\n");
printf("\t\t请输入要添加的学生信息:
\n");xs=(DataType*)malloc(sizeof(DataType));
printf("请输入学生学号\n");scanf("%s",xs->xh);
printf("请输入学生名字\n");scanf("%s",xs->xm);
printf("请输入学生成绩\n");scanf("%d",&xs->cj);
printf("请输入要插入的位置:
\n");
rewind(stdin);
scanf("%d",&i);
cr(L,xs,i);
gotoee;
}
if(q=='4'){rewind(stdin);
printf("查找\n");
printf("查询学生信息\n");i=cz(L);
if(i!
=-1)
printf("%s%s
%d\n",L->data[i].xh,L->data[i].x
m,L->data[i].cj);
}
else
{
printf("
信息不存");
}
gotoee;
}if(q=='5'){rewind(stdin);
printf("
删除\n");
printf("
删除学生信息\n");
sc(L);gotoee;
if(q=='6')
{rewind(stdin);
printf("列表\n");qb(L);
gotoee;
}
if(q=='0'){printf("谢谢使用\n");
}
if(!
(q=='1'||q=='2'||q=='3'||q=='4'||q=='5'||q=='5'||q=='0
'))
{
gotoee;
}
system("pause");
return0;
主程序:
建表:
是否
查找:
是I否
插入:
否
删除:
否
1F
是
是
列表:
实验目的:
学生管理系统(链表)
实验要求:
1.建表
2.删除
3.列表
4.退出
源程序:
#include
#include
#include
structxuesheng
{
charxh[7];
charxm[40];
intcj;
structxuesheng*next;
};
structxuesheng*cha_ru(structxuesheng*x)
{
structxuesheng*p,*q;
intc;
do
{
if(x==0){x=(structxuesheng*)malloc(sizeof(structxuesheng));printf("inputxh:
");scanf("%s",(*x).xh);
printf("inputxm:
");scanf("%s",(*x).xm);
printf("inputcj:
");scanf("%d",&(*x).cj);
(*x).next=0;}
else{p=x;while((*p).next!
=0){p=(*p).next;};q=(structxuesheng*)malloc(sizeof(structxuesheng));printf("inputxh:
");scanf("%s",(*q).xh);printf("inputxm:
");scanf("%s",(*q).xm);printf("inputcj:
");scanf("%d",&(*q).cj);
(*p).next=q;
(*q).next=0;}
printf("nihaijixume?
1/2:
");
scanf("%d",&c);
}
while(c==1);return(x);}
intlink_len(structxuesheng*x)
{
structxuesheng*p;
intl=0;
p=x;if(p!
=0)do{l=l+1;p=p->next;
}while(p!
=0);return(l);
}structxuesheng*shan_chu(structxuesheng*x){structxuesheng*p,*q,*t;
intk,l,i;
p=x;
q=x;l=link_len(x);printf("inputshanchujiedianxuhao:
");scanf("%d",&k);
if(k<=0||k>l)printf("errordata!
\n");if(k==1){x=p->next;
t=p;
free(t);}if(k>1&&k<=l){for(i=1;i<=k-2;i=i+1)p=p->next;
t=p->next;for(i=1;i<=k;i=i+1)q=q->next;p->next=q;
free(t);}printf("vvvvvvvshanchuwanbi!
vvvvvvv\n");return(x);
}
voidlie_biao(structxuesheng*x)
{
structxuesheng*p;
intl;
p=x;
if(p==0)printf("gaibiaoweikong!
\n");
elsedo{
printf("%20s%20s%7d\n",(*p).xh,(*p).xm,(*p).cj);
p=(*p).next;
}
while(p!
=0);l=link_len(x);printf("l=%d\n",l);}
main()
{
structxuesheng*s_head;
intn;
s_head=0;
do
{
printf("1:
charu\n");
printf("2:
chazhao\n");
printf("3:
shanchu\n");
printf("4:
liebiao\n");
printf("5:
tuichu\n");printf("input1---5:
");
scanf("%d",&n);
switch(n)
{
case1:
s_head=cha_ru(s_head);break;
/*case2:
cha_zhao(s_head);break;*/
case3:
s_head=shan_chu(s_head);break;case4:
lie_biao(s_head);break;
}
}while(n==1||n==3||n==4);printf("\n");
return0;
框图:
主函数:
否
建表:
否是
删除:
否
是
总结:
顺序表存储位置是相邻连续的,可以随即访问的一种数据结构,一个顺序表在使用前必须指定起长度,一旦分配内存,则在使用中不可以动态的更改。
他的优点是访问数据是比较方便,可以随即的访问表中的任何一个数据,缺点是定义的长度不可更改造成存储
空间的浪费。
链表是通过指针来描述元素关系的一种数据结构,他可以是物理地址不连续的物理空间。
不能随即访问链表元素,必须从表头开始,一步一步搜索元素。
它的优点是:
对于数组,可以动态的改变数据的长度,分配物理空间。
建议:
在使用中如果一个数组在使用中,查询比较多,而插入,删除数据比较少,数组的长度不变时,选顺序表比较合理。
如果插入,删除,长度不定的数组,可以选链表。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 顺序 表链 总结 实验 报告