链栈

#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--;
}
上一篇:(error) MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk.两种解决方案


下一篇:IDEA快捷键拆解系列(十三):Window篇