zoukankan      html  css  js  c++  java
  • 自定义栈

    public interface IStack<E> {

    /**
    * 压栈,入栈
    * @param e 传入进来的类型
    * @return 是否压栈成功
    */
             public boolean push(E e);

    /**
    * 出栈
    * @return 返回容器中最上面的一个对象
    */
            public E pop();

    /**
    * 返回栈顶的元素,注意这个方法仅仅就只是返回而已
    * @return 栈顶的元素
    */
            public E peek();

    /**
    * 返回指定下标的元素
    * @param index 数组下标
    * @return
    */
            public E get(int index);

    /**
    * 判断是否是空栈
    * @return 如果空返回true,否则返回false
    */
               public boolean isEmpty();

    /**
    * 判断是否满栈,数组长度最大值
    * @return 如果>=当前栈最大容量返回true,否则返回false
    */
             public boolean isMax();

    /**
    * 返回当前栈的长度
    * @return 栈中内容的长度
    */
             public int size();

    /**
    * 查找某个元素在栈中的位置,如果没有返回-1
    * @param e 要查找的元素对象
    * @return 找到返回数组下标,没有找到返回-1
    */
             public int getIndex(E e);

    /**
    * 获取栈最大长度
    * @return 栈能保存数据的最大长度
    */
           public int getStackSize();

    /**
    * 打印方法
    */
            public void display();

    }

    *********************************************************************************************************************************************************************************

    public class MyStack<E> implements IStack<E> {

                  private Object [] data = null;  //数组用于存储数据
                  private int top = -1;     // 记录当前栈的位置(默认-1就是栈顶)
        private int maxSize;   // 栈的最大容量
        private final static int DEFAULT_SIZE = 10;

      public MyStack() {
        this(DEFAULT_SIZE);
      }


      public MyStack(int initiaISize) {
        if (initiaISize > 0) {
          this.data = new Object[initiaISize];
          this.top = -1;
          this.maxSize = initiaISize;
        }else {
          this.data = new Object[DEFAULT_SIZE];
          this.top = -1;
          this.maxSize = DEFAULT_SIZE;
        }
    }

     

      @Override
      public boolean push(E e) {
        if (isMax()) {
          System.out.println("当前栈已经满了....不能放入.....");
          return false;
        }
        top ++;//更新栈顶
        data[top] = e;
        return true;
      }

      @Override
      public E pop() {
        if (isEmpty()) {
          System.out.println("当前栈以空.....");
          return null;
        }
        //先取,再减top
        E e = (E)data[top];
        top --;
        return e;
      }

      @Override
      public E peek() {
        if (isEmpty()) {
          System.out.println("当前栈以空.....");
          return null;
        }
        E e = (E)data[top];
        return e;
      }

      @Override
      public E get(int index) {
        if (index < 0) {
          throw new ArrayIndexOutOfBoundsException();
        }
          E e = (E)data[index];
          return e;
      }

      @Override
      public boolean isEmpty() {
        return this.top == -1 ?true : false;
      }

      @Override
      public boolean isMax() {
        return this.top >=this.maxSize -1 ?true : false;
        }


      @Override
      public int size() {
        return this.top + 1;
      }


      @Override
      public int getIndex(E e) {
        // top的值很关键,不能随意改
          int temp = this.top;
          while (top != -1) {
      if (get(temp).equals(e)) {
          return temp;
        }
        temp --;
      }
      return -1;
     }


      @Override
      public int getStackSize() {
        return this.maxSize;
      }


      @Override
      public void display() {
        int temp = this.top;
        while (temp != -1) {
          System.out.println(this.data[temp]);
          temp --;
        }

            }

    }

    测试:

  • 相关阅读:
    Vue学习笔记【28】——Vue路由(使用 children 属性实现路由嵌套)
    Vue学习笔记【27】——Vue路由(设置路由)
    Vue学习笔记【26】——Vue路由(什么是路由)
    Vue学习笔记【25】——Vue组件(组件间传值)
    Vue学习笔记【24】——Vue组件(组件切换)
    Vue学习笔记【23】——Vue组件(组件的定义)
    ga统计
    token验证机制
    网站发布(项目上线流程)
    使用CSS将图片转换成黑白(灰色、置灰) & 毛玻璃效果
  • 原文地址:https://www.cnblogs.com/suger-4/p/12017294.html
Copyright © 2011-2022 走看看