#include<iostream> using namespace std; struct Node { int data; struct Node*next; }; class LinkStack { public: LinkStack() { top=NULL; } ~LinkStack(){} void Push(int x); int Pop(); int GetTop() { return top->data; } private: Node *top; }; void LinkStack::Push(int x) { Node *s; s=new Node;s->data=x; s->next=top;top=s; } int LinkStack::Pop() { int x; Node * p; if(top==NULL)throw"下溢"; x=top->data;p=top; top=top->next; delete p; return x; } intmain() { LinkStack L; cout<<"Perform stack operations on 13 and 14"<<endl; L.Push(13); L.Push(14); cout<<"The top element of the stack is:"<<endl; cout<<L.GetTop()<<endl; cout<<"Perform a pop operation"<<endl; L.Pop(); cout<<"The top element of the stack is:"<<endl; cout<<L.GetTop()<<endl; return 0; }
chain stack
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=324886818&siteId=291194637
Recommended
Ranking