#include <iostream>
using namespace std;
class node
{
friend class Linkstack; //友元类
private:
long data;
node * next; //递归定义指向后继结点的指针
};
class Linkstack
{
public:
Linkstack(); //创建一个空栈
~Linkstack(); //回收一个栈
void destroy(); //销毁一个栈
void push(long item); //把数据压进栈
void pop(); //出栈
private:
node *Stacktop;
int Stacklength;
};
Linkstack::Linkstack()
{
Stacktop = new node;
Stacklength = 0;
}
Linkstack::~Linkstack()
{
destroy();
delete Stacktop; //释放栈底
}
void Linkstack::destroy() //销毁一个栈
{
for(int i = 0;i < Stacklength; i++)
{
pop();
}
}
/****************进栈***************/
void Linkstack::push(long item) //进栈
{
node *newnodep = new node;
newnodep->data = item;
newnodep->next = Stacktop;
Stacktop = newnodep;
Stacklength++;
}
/****************出栈******************/
void Linkstack::pop()
{
node * popnodep = Stacktop->next ; //定义指针指向出栈结点
delete Stacktop; //释放
Stacktop = popnodep;
Stacklength--;
}
链栈
最新推荐文章于 2020-11-12 12:02:58 发布