stack实现

stack是一种后进先出的数据结构。

template <class T>
class Stack
{
public:
    virtual bool IsEmpty() const=0;
    virtual bool IsFull() const=0;
    virtual bool Top(T &x) const=0;
    virtual bool Push(T x)=0;
    virtual bool Pop()=0;
    virtual void Clear()=0;
};

stack的顺序写法(数组)

#include "stack.h"
 
template <class T>
class SeqStack:public Stack<T>
{
private:
    T *s;
    int top;
    int maxTop;
public:
    SeqStack(int mTop);
    ~SeqStack();
    bool IsEmpty() const;
    bool IsFull() const;
    bool Top(T &x) const;
    bool Push(T x);
    bool Pop();
    void Clear();
    void Print();
};

#include "seqstack.h"
 
template <class T>
SeqStack<T>::SeqStack(int mTop)
{
    maxTop=mTop;
    top=-1;
    s=new T[maxTop];
}
 
template <class T>
SeqStack<T>::~SeqStack()
{
    delete [] s;
}
 
template <class T>
bool SeqStack<T>::IsEmpty() const
{
    if(top<0)
    {
        //cout<<"the seqstack is empty"<<endl;
        return true;
    }
    else
    {
        //cout<<"the seqstack is not empty"<<endl;
        return false;
    }
}
 
template <class T>
bool SeqStack<T>::IsFull() const
{
    if(top>=maxTop-1)
    {
        //cout<<"the seqstack is full"<<endl;
        return true;
    }
    else
    {
        //cout<<"the seqstack is not full"<<endl;
        return false;
    }
}
 
template <class T>
bool SeqStack<T>::Top(T &x) const
{
    if(this->IsEmpty())
    {
        cout<<"the seqstack is empty"<<endl;
        return false;
    }
    else 
    {
        x=s[top];
        return true;
    }
}
 
template <class T>
bool SeqStack<T>::Push(T x)
{
    if(this->IsFull())
    {
        cout<<"the seqstack is full"<<endl;
        return false;
    }
    else
    {
        s[++top]=x;
        return true;
    }
}
 
template <class T>
bool SeqStack<T>::Pop()
{
    if(this->IsEmpty())
    {
        cout<<"the seqstack is empty"<<endl;
        return false;
    }
    else
    {
        top--;
        return true;
    }
}
 
template <class T>
void SeqStack<T>::Clear()
{
    top=-1;
}
 
template <class T>
void SeqStack<T>::Print()
{
    for(int j=0;j<top+1;j++)
    {
        cout<<s[j]<<' ';
    }
    cout<<endl;
}

stack的链接表示(链表)

#include "stack.h"
 
template <class T> class LinkStack;
template <class T>
class Node
{
private:
    Node<T> *link;
    T element;
    friend class LinkStack<T>;
};
 
template <class T>
class LinkStack:public Stack<T>
{
private:
    Node<T> *top;
public:
    LinkStack();
    ~LinkStack();
    bool IsEmpty() const;
    //bool IsFull() const;
    bool Top(T &x) const;
    bool Push(T x);
    bool Pop();
    void Clear();
    void Print();
};

#include "linkstack.h"
 
template <class T>
LinkStack<T>::LinkStack()
{
    top=NULL;
}
 
template <class T>
LinkStack<T>::~LinkStack()
{
    Node<T> *p;
    while(top)
    {
        p=top;
        top=top->link;
        delete p;
    }
    //delete top;
}
 
template <class T>
bool LinkStack<T>::IsEmpty() const
{
    return top==NULL;
}
 
template <class T>
bool LinkStack<T>::Top(T &x) const
{
    if(top)
    {
        x=top->element;
        return true;
    }
    else
        return false;
}
 
template <class T>
bool LinkStack<T>::Push(T x)
{
    Node<T> *p=new Node<T>;
    p->element=x;
    p->link=top;
    top=p;
    return true;
}
 
template <class T>
bool LinkStack<T>::Pop()
{
    if(top)
    {
        Node<T> *p=top;
        top=top->link;
        delete p;
        return true;
    }
    else
        return false;
}
 
template <class T>
void LinkStack<T>::Clear()
{
    Node<T> *p;
    while(top)
    {
        p=top;
        top=top->link;
        delete p;
 
    }
}
 
template <class T>
void LinkStack<T>::Print()
{
    Node<T> *p=top;
    while(p)
    {
        cout<<p->element<<' ';
        p=p->link;
    }
    cout<<endl;
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容