#include<iostream>//链栈,不用设头结点
using namespace std;
struct node
{
int data;
node *next;
};
class Linkstack
{
private:
node *top;//栈顶指针
public:
Linkstack(){
top=NULL;
}
~Linkstack()
{
while(empty()==false)
{
pop();
}
delete top;
}
void push(int x)
{
node *s=new node;
s->data=x;
s->next=top;
top=s;
}
int pop()
{
if(top==NULL)
throw"下溢";
int x=top->data;
node *p=top;
top=top->next;
delete p;
return x;
}
int gettop()
{
int x=top->data;
return x;
}
bool empty()
{
if(top==NULL)
return true;
else return false;
}
};
int main()
{
Linkstack ls;
ls.push(7897);
cout<<"top:"<<ls.gettop()<<endl;
ls.pop();
cout<<"empty():"<<ls.empty()<<endl;
}
推免复习-数据结构-链栈
最新推荐文章于 2022-11-25 13:25:29 发布