欢迎来到天天文库
浏览记录
ID:34708597
大小:51.53 KB
页数:5页
时间:2019-03-09
《严蔚敏版数据结构建立学生信息顺序栈c语言版》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、#include#include#defineSTACK_INIT_SIZE100#defineSTACKINCREMENT10typedefstructStudent/*定义学生类*/{intnum;charname[20];charsex[2];intage;floatgrade;}stu;typedefstruct{stu*base;stu*top;intstacksize;}sqstack;sqstackInitStack()/*构造一个空栈*/{sqstacks;s.base=(stu*)mal
2、loc(STACK_INIT_SIZE*sizeof(stu));if(!s.base)printf("OVERFLOW");else{s.top=s.base;s.stacksize=STACK_INIT_SIZE;printf("OK");returns;}}voidDestroyStack(sqstack*s)/*销毁栈*/{s->base=NULL;free(s->base);printf("OK");}voidClearStack(sqstack*s)/*将栈清空为空栈*/{s->top=s->base;printf("
3、OK");}voidStackEmpty(sqstack*s)/*若为空栈返回TRUE,否则返回FLASE*/{if(s->base==NULL)printf("栈不存在.");elseif(s->top==s->base)printf("TRUE");elseprintf("FLASE");}intStackLength(sqstack*s){if(s->base==NULL)return(-1);elsereturn(s->top-s->base);}stuGetTop(sqstack*s)/*返回栈顶元素*/{if(s-
4、>top==s->base)printf("ERROR");else{printf("OK");return*(s->top-1);}}voidPush(sqstack*s)/*插入新元素*/{stu*newbase;if(s->top-s->base>=s->stacksize){newbase=(stu*)realloc(s->base,(s->stacksize+STACKINCREMENT)*sizeof(stu));if(!newbase)printf("OVERFLOW");else{s->top=s->base+s-
5、>stacksize;s->stacksize=s->stacksize+STACKINCREMENT;printf("请输入插入学生的数据学号:");scanf("%d",&s->top->num);printf("姓名:");scanf("%s",&s->top->name);printf("年龄:");scanf("%d",&s->top->age);printf("性别:");scanf("%s",&s->top->sex);printf("成绩:");scanf("%f",&s->top->grade);s->top++;}}e
6、lse{printf("请输入插入学生的数据学号:");scanf("%d",&s->top->num);printf("姓名:");scanf("%s",&s->top->name);printf("年龄:");scanf("%d",&s->top->age);printf("性别:");scanf("%s",&s->top->sex);printf("成绩:");scanf("%f",&s->top->grade);s->top++;}}stuPop(sqstack*s)/*删除栈顶元素,返回其值*/{stue;if(s->top==
7、s->base)printf("ERROR");else{e=*(s->top-1);s->top=s->top-1;returne;}}voiddisplay(sqstacks)/*输出函数*/{stu*p;p=s.base;printf("***************学生信息*****************");printf("学号姓名性别年龄成绩");for(;pnum,p->name,p->sex,p->age,p->grade)
8、;}printf("****************************************");}voidmain(){sqstacks,*q;stu
此文档下载收益归作者所有