堆栈基本操作2

#include<stdio.h>
#include<conio.h>
#define error 0
#define ok 1
#define equal 1
#define overflow -1
#define stack_init_size 100
#define stackincrement 10
typedef int status;
struct stu{
char name[20];
char stuno[10];
int age;
int score;
};
typedef struct stu selemtype;


struct stack
{
selemtype *base;
selemtype *top;
int stacksize;
};
typedef struct stack sqstack;
status initstack(sqstack **s)
{(*s)=(sqstack *)malloc(sizeof(sqstack));
(*s)->base=(selemtype *)malloc(stack_init_size*sizeof(selemtype));
if(!(*s)->base)exit(overflow);
(*s)->top=(*s)->base;
(*s)->stacksize=stack_init_size;
return ok;
}

status push(sqstack *s,selemtype e)
{
if(s->top -s->base>=s->stacksize)
{
s->base=(selemtype *)realloc(s->base,
(s->stacksize+stackincrement)* sizeof(selemtype));
if(!s->base)exit(overflow);
s->top=s->base+s->stacksize;
s->stacksize+=stackincrement;
}
*(s->top++)=e;
return ok;
}

status pop(sqstack *s,selemtype *e)
{
if(s->top==s->base) return error;
*e=*--s->top;
return ok;
}

main()
{
selemtype e;
sqstack *sa;
clrscr();
printf("first is push.\n");
initstack(&sa);
strcpy(e.name,"stu1");
strcyp(e.stuno,"100001");
e.age=80;
e.score=1000;
push(sa,e);
strcpy(e.name,"stu3");
strcpy(e.stuno,"100002");
e.age=80;
e.score=1000;
push(sa,e);
printf("second is pop.\n");
pop(sa,&e);
printf("%s\n%s\n%d\n%d\n",e.name,e.stuno,e.age,e.score);
pop(sa,&e);
printf("%s\n%s\n%d\n%d\n",e.name,e.stuno,e.age,e.score);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值