栈的顺序实现和链接实现
来源:互联网 发布:中级数据库工程师真题 编辑:程序博客网 时间:2024/06/04 21:25
#ifndef STACK_HPP#define STACK_HPP#include <iostream>#include "exceptions.hpp"template <typename T>/** * @brief The Stack class 栈基类 */class Stack{public: Stack() {} virtual ~Stack(){} /** * @brief lenth 当前的元素个数 * @return */ virtual int lenth() const = 0; /** * @brief push 入栈 * @param t */ virtual void push(const T& t) = 0; /** * @brief pop 出栈 * @return */ virtual T pop() = 0; /** * @brief isEmpty 是否是空栈 * @return */ virtual bool isEmpty() const = 0; /** * @brief isFull 满了 * @return */ virtual bool isFull() const = 0; /** * @brief top 取顶部元素 * @return */ virtual const T& top() const = 0; /** * @brief traverse 遍历 * @param s 分割符 * @param os */ virtual void traverse(const std::string &s, std::ostream &os) const = 0;};#endif // STACK_HPP#ifndef SEQSTACK_HPP#define SEQSTACK_HPP#include "stack.hpp"template <typename T>/** * @brief The SeqStack class 顺序栈 */class SeqStack : public Stack<T>{public: SeqStack(int _defSize = DEFAULTSIZE); ~SeqStack(); int lenth() const { return _top+1; } void push(const T &t); T pop(); const T& top() const; bool isEmpty() const; bool isFull() const { return _top == size-1; } void traverse(const std::string & s = std::string(), std::ostream &os = std::cout) const; int capacity() const { return size; }private: enum {DEFAULTSIZE = 10}; int size; int _top; T *p_d;};template <typename T>SeqStack<T>::SeqStack(int _defSize): size(_defSize), _top(-1){ p_d = new T[size];}template <typename T>SeqStack<T>::~SeqStack(){ delete[] p_d;}template <typename T>void SeqStack<T>::push(const T &t){ if (isFull()) throw OutOfBoundary(); p_d[++_top] = t;}template <typename T>T SeqStack<T>::pop(){ if (isEmpty()) throw OutOfBoundary(); return p_d[_top--];}template <typename T>bool SeqStack<T>::isEmpty() const{ return _top == -1;}template <typename T>const T& SeqStack<T>::top() const{ return p_d[_top];}template <typename T>void SeqStack<T>::traverse(const std::string &s, std::ostream &os) const{ for (int i = 0;i <= _top;++i) { os << p_d[i] << s; }}#endif // SEQSTACK_HPP#ifndef LINKSTACK_HPP#define LINKSTACK_HPP#include "stack.hpp"template <typename T>/** * @brief The LinkStack class 链栈 */class LinkStack : public Stack<T>{public: LinkStack(); ~LinkStack(); void push(const T &t); T pop(); bool isEmpty() const; const T& top() const; void traverse(const std::string &s = std::string(), std::ostream &os = std::cout) const; int lenth() const;private: struct node { T d; node * next; node():next(NULL) { } node(const T& t, node* _next = NULL): d(t), next(_next) { } }; node *_top; int _lenth; bool isFull() const { return false; }};template <typename T>LinkStack<T>::LinkStack(): _top(NULL), _lenth(0){}template <typename T>LinkStack<T>::~LinkStack(){ node *p = NULL; while (_top) { p = _top->next; delete _top; _top = p; }}template <typename T>void LinkStack<T>::push(const T &t){ node *p = new node(t,_top); _top = p; ++_lenth;}template <typename T>T LinkStack<T>::pop(){ if (isEmpty()) throw OutOfBoundary(); T t; t = _top->d; node *p = _top->next; delete _top; _top = p; --_lenth; return t;}template <typename T>inlinebool LinkStack<T>::isEmpty() const{ return _lenth == 0;}template <typename T>inlineint LinkStack<T>::lenth() const{ return _lenth;}template <typename T>const T& LinkStack<T>::top() const { if (isEmpty()) throw OutOfBoundary(); return _top->d;}template <typename T>void LinkStack<T>::traverse(const std::string &s, std::ostream &os) const{ node * p = _top; while(p) { os << p->d << s; p = p->next; }}#endif // LINKSTACK_HPP
类的设计有点问题,那个isFull不应该在基类中
- 栈的顺序实现和链接实现
- 顺序栈和链栈的实现
- 顺序栈的实现和操作
- 顺序栈的实现和操作
- 顺序栈的表示和实现
- 顺序栈的表示和实现
- 栈的顺序表示和实现
- 栈的顺序表示和实现
- 栈的顺序表示和实现
- 栈的顺序表示和实现(数据结构)
- 栈的顺序表示和实现
- 栈的顺序表示和实现
- 栈的顺序存储结构和实现
- 栈的顺序表示和实现
- 顺序栈的设计和实现
- 两种栈的实现--顺序栈和链表栈
- 顺序栈的实现和c++模板
- 顺序栈的表示和实现
- 设计模式:话说分派
- §1 一对多线性回归分析的数学模型
- windows phone:在Windows Phone上使用QQConnect OAuth2
- MSMQ 跨服务器读写队列的“消息队列系统的访问被拒绝”的解决方案
- Eclipse中修改Tomcat的发布路径、发布方式、启动超时等信息
- 栈的顺序实现和链接实现
- IOS简单识记
- c++ builder 如何为application 增加热键(快捷键)
- 素数总结
- SQL Server 2008 重启电脑失败
- §2 回归系数的最小二乘估计
- cocos2d-x的场景
- 安装sql server 2008 报错“检查 Microsoft Visual Studio 2008 的早期版本”失败的问题解
- 学习设计模式六大设计原则之二