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

    public class Stack{
    int[] data;
    int maxSize;
    int top;
    public Stack(int maxSize) {
    this.maxSize=maxSize;
    data=new int[maxSize];
    top=-1;
    }

    //入栈
    public boolean push(int data) {
    //入栈先判断栈中是否已满
    if(top+1==maxSize) {
    System.out.println("栈已满");
    return false;
    }
    this.data[++top]=data;
    return true;
    }

    //出栈
    public int pop() throws Exception{
    //出栈先判断栈是否已空
    if(top==-1) {
    throw new Exception("栈已空");
    }
    return this.data[top--];
    }

    public static void main(String[] args) throws Exception {
    Stack stack=new Stack(1000);
    stack.push(1);
    stack.push(2);
    stack.push(3);
    stack.push(4);
    stack.push(5);
    while(stack.top>=0) {
    System.out.println(stack.pop());
    }
    }

    }

  • 相关阅读:
    第二次作业
    第五次作业
    第四次作业
    第三次作业
    第二次作业
    第三次作业
    第二次作业
    第二次作业
    gravity
    card
  • 原文地址:https://www.cnblogs.com/liuwenwu9527/p/10892368.html
Copyright © 2011-2022 走看看