Etual
全部博文(99)
2012年(3)
2011年(5)
2010年(4)
2009年(31)
2008年(56)
arrvin
wanjin3
道痞
songtao0
晓风凌殇
uijm
cynthia
czllong1
yehuiliu
zhouzhua
lelee007
OsAtNbZS
鹏怜鸿影
分类: C/C++
2009-07-07 21:46:59
#include <stdio.h>#include <stdlib.h>#include <string.h>#define STACK_MAX_SIZE 20typedef int elem_t;typedef struct _stack stack_t;struct _stack{ elem_t data[STACK_MAX_SIZE]; int top_pt;};stack_t *StackCreate(void){ stack_t *s; s = (stack_t *)malloc(sizeof(stack_t)); if (s==NULL) { printf("could not alloc memory for s"); exit(1); } memset(&s->data,0,sizeof(elem_t)*STACK_MAX_SIZE); s->top_pt = 0; return s;}void StackDelete(stack_t *s){ free(s);}int StackIsEmpty(stack_t *s){ return !s->top_pt;}int StackIsFull(stack_t *s){ return s->top_pt == STACK_MAX_SIZE;}void StackPush(stack_t *s,elem_t d){ if(StackIsFull(s)) { printf("stack is full"); StackDelete(s); exit(0); } s->data[s->top_pt] = d; s->top_pt++;}elem_t StackPop(stack_t *s){ elem_t retval; if(StackIsEmpty(s)) { printf("stack is empty"); StackDelete(s); exit(0); } retval = s->data[s->top_pt-1]; s->top_pt--; return retval;}int main(int argc,char *argv[]){ stack_t *st; int i; elem_t retval; st = StackCreate(); for (i=0;i<10;i++) StackPush(st,'0'+i); for (i=0;i<11;i++) { retval = StackPop(st); printf("%d,",retval); } StackDelete(st); return 0;}
上一篇:顺序结构线性表
下一篇:中缀表达式转换
登录 注册