栈的顺序实现和链接实现

来源:互联网 发布:中级数据库工程师真题 编辑:程序博客网 时间: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不应该在基类中
原创粉丝点击