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!
  • 相关阅读:
    03014_properties配置文件
    Python 文件I/O
    Python面向对象
    Python CGI编程
    Python正则表达式
    Python使用SMTP发送邮件
    python操作mysql数据库
    Python多线程
    python XML解析
    给傻瓜用的SP2010开发--第一部分--理解SP开发平台--第一章节--理解SP促销讨论(2)--追踪SP源头
  • 原文地址:https://www.cnblogs.com/hzhida/p/2354739.html
Copyright © 2011-2022 走看看