#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;
}
int main()
{
LinkStack L;
cout<<"对13和14执行入栈操作"<<endl;
L.Push(13);
L.Push(14);
cout<<"栈顶元素为:"<<endl;
cout<<L.GetTop()<<endl;
cout<<"执行一次出栈操作"<<endl;
L.Pop();
cout<<"栈顶元素为:"<<endl;
cout<<L.GetTop()<<endl;
return 0;
}
实验二链栈
最新推荐文章于 2021-02-27 13:36:46 发布