#include
#include
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef struct Student/*定义学生类 */
{
int num;
char name[20];
char sex[2];
int age;
float grade;
}stu;
typedef struct
{
stu *base;
stu *top;
int stacksize;
}sqstack;
sqstack InitStack()/*构造一个空栈*/
{
sqstack s;
s.base=(stu*)malloc(STACK_INIT_SIZE*sizeof(stu));
if(!s.base)
printf("OVERFLOW\n");
else
{
s.top=s.base;
s.stacksize=STACK_INIT_SIZE;
printf("OK\n");
return s;
}
}
void DestroyStack(sqstack *s)/*销毁栈*/
{
s->base=NULL;
free(s->base);
printf("OK\n");
}
void ClearStack(sqstack *s)/*将栈清空为空栈*/
{
s->top=s->base;
printf("OK\n");
}
void StackEmpty(sqstack *s)/*若为空栈返回TRUE,否则返回FLASE*/
{
if(s->base==NULL)
printf("栈不存在.\n");
else if(s->top==s->base)
printf("TRUE\n");
else
printf("FLASE\n");
}
int StackLength(sqstack *s)
{
if(s->base==NULL)
return(-1);
else
return(s->top-s->base);
}
stu GetTop(sqstack *s)/*返回栈顶元素 */
{
if(s->top==s->base)
printf("ERROR\n");
else
{
printf("OK\n");
return *(s->top-1);
}
}
void Push(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\n");
else
{
s->top=s->base+s->stacksize;
s->stacksize=s->stacksize+STACKINCREMENT;
printf("请输入插入学生的数据\n学号:");
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++;
}
}
else
{
printf("请输入插入学生的数据\n学号:");
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++;
}
}
stu Pop(sqstack *s)/*删除栈顶元素,返回其值*/
{
stu e;
if(s->top==s->base)
printf("ERROR\n");
else
{
e=*(s->top-1