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

来源:互联网 发布:sql 恢复删除数据 编辑:程序博客网 时间:2024/06/06 19:23

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

#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");}

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

原创粉丝点击