#include<stdio.h> #include<stdlib.h> #include<malloc.h> #define STACK_INIT_SIZE 100 #define STACKINCREMENT 10 #define OVERFLOW -1 #define OK 1 #define ERROR 0 typedef int Status; typedef int SElemType; typedef struct { SElemType *base; SElemType *top; int stacksize; } SqStack; Status initStack(SqStack &s) { int i; SElemType *p; s.base = (SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType)); if(!s.base)exit(OVERFLOW); s.top = s.base; p=s.base; s.stacksize = STACK_INIT_SIZE; } Status push(SqStack &s,SElemType e) { int *p; int *i; 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) { int *p,*i; if(s.top==s.base)return ERROR; e=*--s.top; return OK; } Status getTop(SqStack s,SElemType &e) { if(s.top==s.base)return ERROR; e=*(s.top-1); printf("%d",e); return OK; } int main() { SqStack s; SElemType e; initStack(s); scanf("%d",&e); push(s,e); getTop(s,e); pop(s,e); }
原文地址:http://blog.csdn.net/whk100312/article/details/26389735