DS:线性表的顺序存储结构实现学生记录表

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

DS:顺序表利用线性表的顺序存储结构实现学生记录表==============================================================================================================Node.h==================================================#defineListSize20typedefstructrecord{charnumber[6];charname[10];intage;floatscore;}DataType;typedefstruct{DataTyperecords[ListSize];intlength;}SeqList;====================================================================================================================================================================================================================================================================================SeqList_Basic.h=========================================#includestdio.h#includestring.h#includeNode.hvoidInitList(SeqList*L){L-length=0;}DataTypeWriteInfo()//向一个记录节点中写入信息{intj;DataTypetemp;printf(\n\n\t\t\tPleaseinputanewstudent'sinformation:\n);for(j=1;j=4;j++)//给第i个学生输入信息{if(j==1){printf(inputNumber(5figures):);scanf(%s,temp.number);}elseif(j==2){printf(inputName:);scanf(%s,temp.name);}elseif(j==3){printf(inputAge:);scanf(%d,&temp.age);}else{printf(inputScore:);scanf(%f,&temp.score);}}returntemp;}voidStore(SeqList*L){intn,i;voidInsertList(SeqList*L,inti,DataTypex);//插入函数的声明printf(Pleaseinputthenumberofrecords:);scanf(%d,&n);for(i=0;in;i++)//每循环一次存储一个学生的信息{intj;DataTypetemp;printf(\n\n\t\t\tPLEASEINPUTtheINFORMATIONofSTUDENT%d\n,i+1);for(j=1;j=4;j++)//给第i个学生输入信息{if(j==1){printf(inputNumber(5figures):);scanf(%s,temp.number);}elseif(j==2){printf(inputName:);scanf(%s,temp.name);}elseif(j==3){printf(inputAge:);scanf(%d,&temp.age);}else{printf(inputScore:);scanf(%f,&temp.score);}}InsertList(L,i+1,temp);//向记录表中插入第i个学生的信息;经过n次插入后,//n个学生的信息就存储在记录表里面了。}}voidShow(SeqList*L){inti;printf(\t\t\t\t学号:\t姓名:\t年龄:\t学分:\n);for(i=0;iL-length;i++)printf(\t\t\t\t%s\t%s\t%3d\t%6.2f\n,L-records[i].number,L-records[i].name,L-records[i].age,L-records[i].score);}voidInsertList(SeqList*L,inti,DataTypex){intj;if(i1||iL-length+1)//插入位置的检查{printf(positionerror!\n);return;}if(L-length=ListSize)//剩余存储空间检查{printf(overflow!\n);return;}for(j=L-length-1;j=i-1;j--)//后移元素,留出插入位置L-records[j+1]=L-records[j];L-records[i-1]=x;//插入元素L-length++;//修改表长}intListLength(SeqList*L){returnL-length;}//voidLocateList(SeqList*L,char)//{DataTypeGetNode(SeqList*L,inti)//获取第i个学生的信息{if(i=0||iL-length){printf(PositionError\n);return;//这个位置按理说应该返回一个错误信息的,但是不知道这个错误信息该怎么写,仅用Error来代替那是伪代码做的事情。}returnL-records[i-1];}DataTypeLocateNode(SeqList*L,char*name){inti;for(i=0;iL-length;i++)if(strcmp(name,L-records[i].name)==0)returnL-records[i];}DataTypeDeleteList(SeqList*L,inti){intj;DataTypetemp;if(i1||iL-length){printf(PostionError!\n);return;}temp=L-records[i-1];for(j=i;j=L-length;j++)L-records[j-1]=L-records[j];L-length--;returntemp;}===========================================================================================================================================================================================================================================================================================Main.c=============================================#includestdlib.h#includeSeqList_Basic.hvoidmain(){intposition;char*name=(char*)malloc(6*sizeof(char));DataTypetemp;SeqListstudent;//生成一个名为student的记录表InitList(&student);//对新生成的记录表置空Store(&student);//向记录表中存储数据Show(&student);//刷新记录表中的信息printf(\n\t\t\t\tThelengthofthisrecordis%d\n,ListLength(&student));//计算学生记录个数printf(Pleaseinputastudent'spositiontogethisinformation:);scanf(%d,&position);temp=GetNode(&student,position);//获取第i个学生的信息printf(\t\t\t\t%s\t%s\t%3d\t%6.2f\n,temp.number,temp.name,temp.age,temp.score);printf(Pleaseinputastudent'snametogethisinformation:);scanf(%s,name);temp=LocateNode(&student,name);//根据姓名获取学生信息printf(\t\t\t\t%s\t%s\t%3d\t%6.2f\n,temp.number,temp.name,temp.age,temp.score);printf(Pleaseinputthestudent'spositiontoinsertanewrecord:);scanf(%d,&position);//printf(andthenpleaseinputthenewinformation:);//这句可以看作是多余的,因为WriteInfo函数中已有该提示语句。temp=WriteInfo();InsertList(&student,position,temp);//向第i个学生之前插入一个信息为temp的新学生,并把记录表长加1Show(&student);//显示插入后的新记录表printf(Pleaseinputthestudent'spositiontodeletehisrecord:);scanf(%d,&position);temp=DeleteList(&student,position);//删除记录表中的第i个学生的信息,记录表长度减1Show(&student);}==========================================================================================================================================================================

1 / 7
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功