1 //存储空间大小宏定义 2 #define STACK_INIT_SIZE 100 3 #define STACKINCREMENT 10 4 5 //结构体定义 6 typedef struct{ 7 SElemType * base; 8 SElemType * top; 9 int StackSize; 10 }SqStack; 11 12 //方法声明 13 status InitStack(SqStack &s); 14 status Push(SqStack &s,SElemType e); 15 status Pop(Sqstack &s,SElemType& e); 16 17 //方法定义 18 status InitStack(SqStack &s) 19 { 20 s.base=(SElemType*)malloc(sizeof(SElemType)*STACK_INIT_SIZE); 21 s.top=s.base; 22 s.StackSize=STACK_INIT_SIZE; 23 return ok; 24 } 25 26 status Push(SqStack &s,SElemType e) 27 { 28 if(s.top-s.base>=s.StackSize) 29 { 30 s.base=(SElemType*)realloc(s.base, (STACK_INIT_SZIE+STACKINCREMENT)*sizeof(SElemType)); 31 s.top=s.base+s.StackSize; 32 s.Stacksize+=STACKINCREMENT; 33 } 34 35 *s.top++=e; 36 37 return ok; 38 } 39 40 41 status Pop(Sqstack &s,SElemType& e) 42 { 43 if(s.base==s.top) 44 return ERROR; 45 e=*(--s.top); 46 return ok; 47 }