#include <iostream> #include <string> #include <vector> #include <algorithm> #define MAXSIZE 100 using namespace std;/*鏈棧 */typedef struct StackNode{ int data; struct StackNode *next;}StackNode,*LinkStack;//初始化,不帶頭結點 int InitStack(LinkStack &S){ S = NULL; return 1; } //入棧(頭插入法) int push(LinkStack &S,int e) { LinkStack p = new StackNode; p->data = e; p->next = S; S = p; return 1; } //取棧頂元素 int getTop(LinkStack S) { if(S!=NULL) { return S->data; } else { return -1; } } //出棧 int pop(LinkStack &S) { if(S==NULL) { cout<<"int pop(LinkList &S) err:棧空error"<<endl; return 0; } else { LinkStack p = S; S = S->next; delete p; return 1; } } int main(){ LinkStack stack; InitStack(stack); push(stack,1); push(stack,2); push(stack,3); push(stack,4); push(stack,5); cout<<"getTop:"<<getTop(stack)<<endl; pop(stack); cout<<"getTop:"<<getTop(stack)<<endl; }
新聞熱點
疑難解答