#include <stdio.h> #include <tchar.h> #include <stdlib.h> #define StackSize 5 #define IncrementSize 5 // TODO: 在此处引用程序需要的其他头文件 struct Stack { int *base; int *top; int stacksize; }; //初始化栈 bool Init_Stack(Stack* s){ s->base = (int*)malloc(StackSize*sizeof(int)); if (s->base == NULL) return false; s->top = s->base; s->stacksize = StackSize; return true; } //销毁栈 bool Destroy_Stack(Stack* s){ free(s->base); s->base = NULL; s->top = NULL; s->stacksize = 0; return true; } //清空栈 bool Clear_Stack(Stack* s){ s->top = s->base; return true; } //插入元素 bool Push(Stack* s, int value){ if (s->top - s->base >= s->stacksize){ s->base = (int*)realloc(s->base, (StackSize + IncrementSize)*sizeof(int)); s->top = s->base + s->stacksize;//重置一下栈顶,感觉也没多大必要呀 s->stacksize += IncrementSize; } *++s->top = value;//栈顶指向栈顶元素 s->stacksize++; return true; } //弹出元素 int Pop(Stack* s){ if (s->top == s->base) return false; int p = *s->top--; s->stacksize--; return p; } //逆序打印 void Print_Stack(Stack s){ if (s.top == s.base) printf("此栈为空 "); while (s.top!=s.base){ printf("%d->", Pop(&s)); } printf(" "); } //正序打印 void Stack_Traverse(Stack s){ if (s.top == s.base) printf("此栈为空 "); while (s.top != s.base){ printf("%d->", *++s.base); } printf(" "); }