数据结构之 栈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");}
说明一下,最近写的比往常频繁了,倒不是因为自己要奋发什么的,只是最近看书看到需要练笔的地方,而且恰好自己能够写出来。
阅读全文
0 0
- 数据结构之 栈stack 模板类(链表表示)
- 数据结构之 栈stack 模板类(数组描述方法)
- 栈(stack)的链表表示
- 数据结构之图的十字链表表示法
- 数据结构之 队列 链表表示与数组表示
- 数据结构之栈stack
- 数据结构-十字链表表示矩阵(C++)
- C++数据结构栈Stack的实现模板类
- C++数据结构之Stack(栈)
- 基本数据结构之Stack类(一)
- 【数据结构】双向链表表示和实现
- C++之Stack模板类
- 基本数据结构之栈(stack)
- Java 数据结构之 Stack(栈)
- JavaScript数据结构之--------栈(Stack)
- 栈(stack )数据结构
- 40. 数据结构笔记之四十图的邻接多重链表表示实现
- 数据结构之图的邻接表表示法
- 大数据Spark03_集群环境搭建及任务提交的方式
- Netty4实战第十六章:注销/注册EventLoop
- java基础的小细节2-集合框架
- 换用Laravel后,PHP进行纯文本书写时,丢失换行符的问题
- 一起Talk Android吧(第四十六回:Android中的Fragment十一)
- 数据结构之 栈stack 模板类(链表表示)
- 进程地址空间分布和可执行文件分布
- 大型网站的灵魂——性能
- UDP与TCP穿洞技术学习笔记
- 2311: [CEOI2016]D2T3 Router
- 读书笔记2 MYSQL必知必会
- ssh原理与运用
- Lintcode-动态规划-最长上升连续子序列
- Servlet生命周期