顺序表链表总结实验报告.docx

上传人:b****5 文档编号:4389673 上传时间:2022-12-01 格式:DOCX 页数:24 大小:278.94KB
下载 相关 举报
顺序表链表总结实验报告.docx_第1页
第1页 / 共24页
顺序表链表总结实验报告.docx_第2页
第2页 / 共24页
顺序表链表总结实验报告.docx_第3页
第3页 / 共24页
顺序表链表总结实验报告.docx_第4页
第4页 / 共24页
顺序表链表总结实验报告.docx_第5页
第5页 / 共24页
点击查看更多>>
下载资源
资源描述

顺序表链表总结实验报告.docx

《顺序表链表总结实验报告.docx》由会员分享,可在线阅读,更多相关《顺序表链表总结实验报告.docx(24页珍藏版)》请在冰豆网上搜索。

顺序表链表总结实验报告.docx

顺序表链表总结实验报告

实验报告

实验目的:

学生管理系统(顺序表)

实验要求:

1.建表

2.求表长

3.插入

4.查找

5.删除

6.列表

7.退出

源程序:

#include

#include

#include

#defineMaxSize1000

typedefstruct

{

charxh[40];

charxm[40];

intcj;

}DataType;//学生的结构

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;klength;k++)

{

w=k+1;

printf("第%d位学生:

",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、按学号查询\n");

printf("1、按姓名查询\n");

printf("1、按成绩查询\n");

printf("请选择:

");

fflush(stdin);

scanf("%d",&u);

if(u==1)

{

printf("请输入要查找学生的学号:

");

scanf("%s",xh);

for(i=0;ilength;i++)

{

if(strcmp(L->data[i].xh,xh)==0)

returni;

}

}

if(u==2)

{

printf("请输入要查找学生的姓名:

");

scanf("%s",xm);

for(i=0;ilength;i++)

{

if(strcmp(L->data[i].xm,xm)==0)

returni;

}

}

if(u==3)

{

printf("请输入要查找学生的成绩:

");

scanf("%s",cj);

for(i=0;ilength;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;ilength;i++)

{

if(strcmp(L->data[i].xh,xh)==0)//判断输入和已知学号一样不

returni;

}

}

elseif(h==2)

{

printf("请输入要删除学生的姓名:

");

scanf("%s",xm);

for(i=0;ilength;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;jlength;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("退出请按0\n");

printf("请输入");

scanf("%c",&q);

}

if(q=='1')

{

rewind(stdin);

liebiao(L);

gotoee;

 

}

if(q=='2')

{

rewind(stdin);

bc(L);

printf("共%d个学生\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].xm,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;

}

 

主程序:

 

建表:

 

是否

 

 

查找:

 

是否

 

插入:

 

是否

 

 

删除:

 

是否

 

否是

 

列表:

 

是否

 

实验目的:

学生管理系统(链表)

实验要求:

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;

}

 

框图:

主函数:

是否

 

建表:

否是

 

 

删除:

是否

是否

 

总结:

顺序表存储位置是相邻连续的,可以随即访问的一种数据结构,一个顺序表在使用前必须指定起长度,一旦分配内存,则在使用中不可以动态的更改。

他的优点是访问数据是比较方便,可以随即的访问表中的任何一个数据,缺点是定义的长度不可更改造成存储空间的浪费。

链表是通过指针来描述元素关系的一种数据结构,他可以是物理地址不连续的物理空间。

不能随即访问链表元素,必须从表头开始,一步一步搜索元素。

它的优点是:

对于数组,可以动态的改变数据的长度,分配物理空间。

建议:

在使用中如果一个数组在使用中,查询比较多,而插入,删除数据比较少,数组的长度不变时,选顺序表比较合理。

如果插入,删除,长度不定的数组,可以选链表。

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 高中教育 > 数学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1