zoukankan      html  css  js  c++  java
  • template<class Elem>
    class AStack:public Stack<Elem> //数组实现的栈类
    {

    private:
    int size;
    int top;
    Elem *listArray;
    public:
    AStack(int sz=DefaultListSize)
    {
    size=sz;top=0;listArray=new Elem[sz];
    }
    ~AStack()
    {
    delete [] listArray;
    }
    void clear()
    {
    top=0;
    }
    bool push(const Elem& item)
    {
    if(top==size) return false;
    else
    {
    listArray[top++]=item;
    }
    }
    bool pop(Elem & it)
    {
    if(top==0) return false;
    else
    {
    it=listArray[--top];
    return true;
    }
    }
    bool topValue(Elem & it) const
    {
    if(top==0) return false;
    else
    {
    it=listArray[top-1];
    return true;
    }
    }
    int length() const
    {
    return top;
    }
    };



    #include "LList.h"
    template<class Elem>
    //用链表实现的栈
    class LStack:public Stack<Elem>

    {
    private:
    Link<Elem> * top;
    int size;
    public:
    LStack(int sz=DefaultListSize)
    {
    top=NULL;size=0;
    }
    ~LStack()
    {
    clear();
    }
    void clear()
    {
    while(top!=NULL)
    {
    Link<Elem> *temp=top;
    top=top->next;
    size=0;
    delete temp;
    }
    }
    bool push(const Elem& item)
    {
    top=new Link<Elem>(item,top);
    size++;
    return true;
    }
    bool pop(Elem & it)
    {
    if(size==0) return false;
    it=top->element;
    Link<Elem> * ltemp=top->next;
    delete top;
    top=ltemp;
    size--;
    return true;
    }
    bool topValue(Elem &it) const
    {
    if(size==0) return false;
    it=top->element;
    return true;
    }
    int length() const
    {
    return size;
    }
    };
    Live together,or Die alone!
  • 相关阅读:
    代码高亮测试
    自定义Edit控件控制输入范围
    多字节字符与界面 manifest
    实现类成员函数回调
    [VIM插件]fedora22编译vim7.4对perl组件支持的问题
    火车头Ecshop2.7文章采集发布模块
    js 创建对象
    js 属性类型
    JS函数的属性
    JS 函数中返回另一个函数
  • 原文地址:https://www.cnblogs.com/hzhida/p/2354739.html
Copyright © 2011-2022 走看看