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!
  • 相关阅读:
    Set和Map
    将博客搬至CSDN
    (转)VS制作安装包
    C#俄罗斯方块实现思路及源码
    ASP.NET网站部署过程
    2016百度之星资格赛总结
    数据库编程常见错误总结
    (转)Android 如何全局获取Context
    Android数据文件存储
    关于工程文档中图表的使用
  • 原文地址:https://www.cnblogs.com/hzhida/p/2354739.html
Copyright © 2011-2022 走看看