数据结构之 栈stack 模板类(链表表示)

使用链表表示的栈结构模板类程序如下所示

#ifndef _H_MYSTACK_LIST_H
#define _H_MYSTACK_LIST_H

/宏定义栈的临界长度,超过此长度之后需要在利用率较低的情况下回收空间
#define BOUNDARY_LENGTH 2048

#include <iostream>

template<class T>
class myStack_list;

template<class T>
struct snode
{
    struct snode<T>* next;
    T val;
};

template<class T>
class myStack_list
{
public:
    myStack_list(void);
    myStack_list(const myStack_list<int>& st);
    ~myStack_list();

    void push(const T val);/头插法压栈
    T pop(void);/弹出栈顶元素(删除栈顶元素,如果不存在,则throw异常)
    int clear(void);清空,并且返回元素数目
    T top(void);///仅仅返回栈顶元素,为空则抛出异常

    int size(void);/计算元素的数目
    bool isEmpty(void);///指示栈是否为空
    myStack_list<T>& operator=(const myStack_list<int>& st);
private:
    struct snode<T>* m_pHead;
};

template<class T>
myStack_list<T>::myStack_list(void)
{
    m_pHead = NULL;
}

template<class T>
myStack_list<T>::myStack_list(const myStack_list<int>& st)
{
    m_pHead = NULL;
    struct snode<T>* p = st.m_pHead;
    struct snode<T>* p_last = NULL;
    while (NULL != p)
    {
        if(NULL == m_pHead)
        {
            m_pHead = new struct snode<T>;
            m_pHead->val = p->val;
            m_pHead->next = NULL;
            p_last = m_pHead;
        }
        else
        {
            p_last->next = new struct snode<T>;
            p_last = p_last->next;
            p_last->next = NULL;
            p_last->val = p->val;
        }
        p = p->next;
    }
}

template<class T>
myStack_list<T>::~myStack_list()
{
    struct snode<T>* p = m_pHead;
    while (NULL != p)
    {
        struct snode<T>* p_nxt = p->next;
        delete p;
        p = p_nxt;
    }
    m_pHead = NULL;
}

template<class T>
void myStack_list<T>::push(const T val)
{
    struct snode<T>* p = new struct snode<T>;
    p->val = val;
    p->next = m_pHead;
    m_pHead = p;/头插法
}

template<class T>
T myStack_list<T>::pop(void)/弹出栈顶元素(删除栈顶元素,如果不存在,则throw异常)
{
    if (NULL == m_pHead)
    {
        throw("NoElmentInStack");
    }
    struct snode<T>* p = m_pHead;
    m_pHead = m_pHead->next;
    T val = p->val;
    delete p;
    return val;
}

template<class T>
int myStack_list<T>::clear(void)清空,并且返回元素数目
{
    struct snode<T>* p = m_pHead;
    int cnt = 0;
    while (NULL != p)
    {
        cnt = cnt + 1;
        struct snode<T>* p_nxt = p->next;
        delete p;
        p = p_nxt;
    }
    m_pHead = NULL;
    return cnt;
}

template<class T>
T myStack_list<T>::top(void)///仅仅返回栈顶元素,为空则抛出异常
{
    return(m_pHead->val);
}

template<class T>
int myStack_list<T>::size(void)/计算元素的数目
{
    struct snode<T>* p = m_pHead;
    int cnt = 0;
    while (NULL != p)
    {
        cnt = cnt + 1;
        p = p->next;
    }
    return cnt;
}

template<class T>
bool myStack_list<T>::isEmpty(void)///指示栈是否为空
{
    return(NULL == m_pHead);
}

template<class T>
myStack_list<T>& myStack_list<T>::operator=(const myStack_list<int>& st)
{
    clear();首先清除原有的数据.clear里面已经将头结点置为NULL

    struct snode<T>* p = st.m_pHead;
    struct snode<T>* p_last = NULL;
    while (NULL != p)
    {
        if(NULL == m_pHead)
        {
            m_pHead = new struct snode<T>;
            m_pHead->val = p->val;
            m_pHead->next = NULL;
            p_last = m_pHead;
        }
        else
        {
            p_last->next = new struct snode<T>;
            p_last = p_last->next;
            p_last->next = NULL;
            p_last->val = p->val;
        }
        p = p->next;
    }
    return(*this);
}
#endif

使用到的测试程序如下所示(简单测试了一下,可能不全面)

#include <iostream>
#include "myStack_list.cpp"

void main(void)
{
    myStack_list<int> mst;
    for (int ii = 0; ii < 10; ii++)
    {
        mst.push(ii * ii);
    }

    int topVal = 0;
    myStack_list<int> mst2(mst);
    myStack_list<int> mst3;
    mst3 = mst2;
    while (!mst.isEmpty())
    {
        topVal = mst.pop();
        std::cout<<topVal<<"\t";
    }
    std::cout<<std::endl;

    while (!mst2.isEmpty())
    {
        topVal = mst2.pop();
        std::cout<<topVal<<"\t";
    }
    std::cout<<std::endl;

    while (!mst3.isEmpty())
    {
        topVal = mst3.pop();
        std::cout<<topVal<<"\t";
    }
    std::cout<<std::endl;
    std::system("pause");
}

说明一下,最近写的比往常频繁了,倒不是因为自己要奋发什么的,只是最近看书看到需要练笔的地方,而且恰好自己能够写出来。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值