链栈的建立与操作

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;

template<class ElemType>
struct Node
{
    ElemType data;
    Node<ElemType> *next;
    Node();
    Node(ElemType item,Node<ElemType> *link);
};

template<class ElemType>
Node<ElemType>::Node()
{
    next=NULL;
}

template<class ElemType>
Node<ElemType>::Node(ElemType item,Node<ElemType> *link)
{
    data=item;
    next=link;
}

template<class ElemType>
class LinkStack
{
protected:
    Node<ElemType>*top;
    int cnt;
public:
    LinkStack();
    ~LinkStack();
    int Length() const;
    bool Empty() const;
    void Clear();
    void Traverse() const;
    bool Push(const ElemType &e);
    bool Top(ElemType &e) const;
    bool Pop(ElemType &e);
    LinkStack(const LinkStack<ElemType> &copy);
    LinkStack<ElemType> &operator=(const LinkStack<ElemType> &copy);
};

template<class ElemType>
LinkStack<ElemType>::LinkStack()
{
    top=NULL;
    cnt=0;
}

template<class ElemType>
LinkStack<ElemType>::~LinkStack()
{
    Clear();
}

template<class ElemType>
int LinkStack<ElemType>::Length() const
{
    return cnt;
}

template<class ElemType>
bool LinkStack<ElemType>::Empty() const
{
    return top==NULL;
}

template<class  ElemType>
void LinkStack<ElemType>::Clear()
{
    ElemType tmpElem;
    while(!Empty())
    {
        Pop(tmpElem);
    }
}

template<class ElemType>
void LinkStack<ElemType>::Traverse() const
{
    Node<ElemType> *tmpPtr;
    LinkStack<ElemType>tmpS;
    for(tmpPtr=top;tmpPtr!=NULL;tmpPtr=tmpPtr->next)
    {
        tmpS.Push(tmpPtr->data);
    }

    for(tmpPtr=tmpS.top;tmpPtr!=NULL;tmpPtr=tmpPtr->next)
    {
        cout<<tmpPtr->data<<' ';
    }
    cout<<endl;
}

template<class ElemType>
bool LinkStack<ElemType>::Push(const ElemType &e)
{
    Node<ElemType>*newTop=new Node<ElemType>(e,top);
    if(newTop==NULL)
    {
        return false;
    }
    else
    {
        top=newTop;
        cnt++;
        return true;
    }
}

template<class ElemType>
bool LinkStack<ElemType>::Top(ElemType &e) const
{
    if(Empty())
    {
        return false;
    }
    else
    {
        e=top->data;
        return true;
    }
}

template<class ElemType>
bool LinkStack<ElemType>::Pop(ElemType &e)
{
    if(Empty())
    {
        return false;
    }
    else
    {
        Node<ElemType> *old_top=top;
        e=old_top->data;
        top=old_top->next;
        cnt--;
        delete old_top;
        return true;
    }
}

template<class ElemType>
LinkStack<ElemType>::LinkStack(const LinkStack<ElemType> &copy)
{
    if(copy.Empty())
    {
        top=NULL;
        cnt=0;
    }
    else
    {
        top=new Node<ElemType>;
        top->data=copy.top->data;
        cnt=copy.cnt;
        Node<ElemType> *buttonPtr=top;
        for(Node<ElemType> *tmpPtr=copy.top->next;tmpPtr!=NULL;tmpPtr=tmpPtr->next)
        {
            buttonPtr->next=new Node<ElemType>;
            buttonPtr->next->data=tmpPtr->data;
            buttonPtr=buttonPtr->next;
        }
    }
}

template<class ElemType>
LinkStack<ElemType>&LinkStack<ElemType>::operator=(const LinkStack<ElemType> &copy)
{
    if(&copy!=this)
    {
        if(copy.Empty())
        {
            top=NULL;
            cnt=0;
        }
        else
        {
            Clear();
            top=new Node<ElemType>;
            top->data=copy.top->data;
            cnt=copy.cnt;

            Node<ElemType>* buttomPtr=top;
            for(Node<ElemType>*tmpPtr=copy.top->next;tmpPtr!=NULL;tmpPtr=tmpPtr->next)
            {
                buttomPtr->next=new Node<ElemType>;
                buttomPtr->next->data=tmpPtr->data;
                buttomPtr=buttomPtr->next;
            }
        }
    }
    return *this;
}

int main()
{
    LinkStack<int>s;
    int i;
    for(i=1;i<=5;i++)
    {
        s.Push(i);
    }
    s.Traverse();

    LinkStack<int>s1(s);
    s1.Traverse();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值