资源描述:
《严蔚敏版数据结构学生信息链栈.doc》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、#include#include#includetypedefstructStudent/*定义学生类*/{intnum;charname[20];charsex[2];intage;floatgrade;}stu;typedefstructSNode{studata;structSNode*next;}*linkstack,SNode;linkstackInitStack()/*构造一个空栈*/{linkstacks;s=(linkstack)malloc(si
2、zeof(SNode));if(!s)printf("ERROR");else{s->next=NULL;printf("OK");returns;}}voidDestroyStack(linkstacks)/*销毁栈*/{linkstackp;if(!s)printf("ERROR");else{while(s){p=s;s=s->next;free(p);}printf("OK");}}linkstackClearStack(linkstacks)/*将栈清空*/{linkstackp;if(!s)p
3、rintf("ERROR");else{while(s->next){p=s;s=s->next;free(p);}printf("OK");returns;}}voidStackEmpty(linkstacks)/*空栈返回TRUE,否则返回FLASE*/{if(!s)printf("ERROR");elseif(!s->next)printf("TRUE");elseprintf("FLASE");}intStackLength(linkstacks)/*返回栈长度*/{linkstackp=s;
4、inti=0;if(!s)return-1;else{while(p->next){i++;p=p->next;}returni;}}stuGetTop(linkstacks)/*返回栈顶元素*/{if(!s->next)printf("ERROR");else{printf("OK");returns->data;}}linkstackPush(linkstacks)/*插入新元素*/{linkstackp;p=(linkstack)malloc(sizeof(SNode));if(!p){printf("OV
5、ERFLOW");returns;}else{printf("请输入插入学生的数据学号:");scanf("%d",&p->data.num);printf("姓名:");scanf("%s",&p->data.name);printf("年龄:");scanf("%d",&p->data.age);printf("性别:");scanf("%s",&p->data.sex);printf("成绩:");scanf("%f",&p->data.grade);p->next=s;s=p;printf("OK")
6、;returns;}}linkstackPop(linkstacks)/*删除栈顶元素*/{stue;linkstackp;if(!s->next){printf("ERROR");returns;}else{e=s->data;p=s;s=s->next;free(p);printf("OK");printf("删除的栈顶学生信息为:学号:%d姓名:%s性别:%s年龄:%d成绩:%f删除后表为:",e.num,e.name,e.sex,e.age,e.grade);returns;}
7、}voiddisplay(linkstacks)/*输出函数*/{linkstackp;p=s;printf("***************学生信息*****************");printf("学号姓名性别年龄成绩");if(p){while(p->next){printf("%dt%st%st%dt%f",p->data.num,p->data.name,p->data.sex,p->data.age,p->data.grade);p=p->next;}}printf("********
8、********************************");}voidmain(){linkstacks;stue;intn,i,j;s=InitStack();printf("请输入任意键进入菜单..........");getchar();for(;;){printf("************