数据结构之 栈stack 模板类(链表表示)
2017-10-29 21:02
253 查看
使用链表表示的栈结构模板类程序如下所示
使用到的测试程序如下所示(简单测试了一下,可能不全面)
说明一下,最近写的比往常频繁了,倒不是因为自己要奋发什么的,只是最近看书看到需要练笔的地方,而且恰好自己能够写出来。
#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_p 4000 Head = 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"); }
说明一下,最近写的比往常频繁了,倒不是因为自己要奋发什么的,只是最近看书看到需要练笔的地方,而且恰好自己能够写出来。
相关文章推荐
- 数据结构之 队列 链表表示与数组表示
- 图解数据结构(1)——大圈表示法、动态数组和单向链表
- 数据结构之---C语言实现广义表头尾链表存储表示
- 图解数据结构(1)——大圈表示法、动态数组和单向链表
- 看数据结构写代码(52) 广义表的扩展线性链表存储表示
- 数据结构之 栈stack 模板类(数组描述方法)
- 数据结构之图的链表表示
- 数据结构(二):线性表的使用原则以及链表的应用-稀疏矩阵的三元组表示
- 数据结构(Data structure):用链表实现多项式的表示和运算(C语言)
- 图解数据结构(1)——大圈表示法、动态数组和单向链表
- 图解数据结构(1)——大圈表示法、动态数组和单向链表
- 数据结构算法代码实现——线性表的链式表示与实现(单链表)(三 )
- 图解数据结构(1)——大圈表示法、动态数组和单向链表
- 40. 数据结构笔记之四十图的邻接多重链表表示实现
- 看数据结构写代码(60 ) 键树的多重链表表示(Trie树)
- 图解数据结构(1)——大圈表示法、动态数组和单向链表
- 数据结构——二叉树的孩子链表表示法
- 数据结构——图的链表实现(邻接表表示法)
- 数据结构——链表—一元多项式的表示及相加
- 算法:一元多项式的表示及相加(链表实现)-数据结构(4)