#include <iostream> #include <string> #include <vector> #include <algorithm> #define MAXSIZE 100 using namespace std;/*順序棧 */typedef struct{ int *base;//棧底指針 int *top;//棧頂指針 int stacksize;//最大容量 }SqStack;//初始化int InitStack(SqStack &S){ S.base = new int[MAXSIZE]; if(!S.base) { cout<<"method InitStack() err:內存申請error!"<<endl; return 0; } S.top = S.base; S.stacksize = MAXSIZE; } //入棧 int push(SqStack &S,int e) { if(S.top-S.base==MAXSIZE) { cout<<"method push() err:棧滿error!"<<endl; return 0; } *(S.top++) = e; return 1; } //取棧頂元素 int getTop(SqStack S) { if(S.top!=S.base) { return *(S.top-1); } else return -1; } //出棧 int pop(SqStack &S) { if(S.top==S.base) { cout<<"method pop() err:??誩rror"<<endl; return 0; } int e = *(--S.top); return 1; } int main(){ SqStack stack; InitStack(stack); push(stack,1); push(stack,2); push(stack,3); push(stack,4); push(stack,5); pop(stack); cout<<getTop(stack)<<endl;}
新聞熱點
疑難解答