简单栈的实现

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_43765564/article/details/89608044
package jing.able.impl;

/**
 * @author: panjing
 * @describe:
 * @date: 2019/4/14
 * @time: 15:07
 */
public interface IMyStack {
    // 将 item 压入栈中
    void push(int item);
    // 返回栈顶元素,并且出栈
    int pop();
    // 返回栈顶元素,但不出栈
    int peek();
    // 判断这个栈是否为空栈
    boolean empty();
    // 返回元素个数
    int size();
}
  66  MyStack.java 
@@ -0,0 +1,66 @@
package jing.able.dao;

import jing.able.impl.IMyStack;

/**
 * @author: panjing
 * @describe:   简单栈的实现
 * @date: 2019/4/14
 * @time: 15:08
 */

public class MyStack implements IMyStack {
    private int[] elem;
    private int top; //保存得是当前可以存放数据源数的下标
    private int usedSize;
    //默认栈的容量
    private static final int DEFAULT_SIZE=10;  //栈的长度
    public MyStack(){
        this.elem = new int[DEFAULT_SIZE];
        this.top = 0;
        this.usedSize = 0;
    }

    public boolean isFull(){
        return this.top== this.elem.length;
    }
    @Override
    public void push(int item) {
        if(isFull()) {
          throw new UnsupportedOperationException("栈为满");
        }
        this.elem[this.top++] =item;
    }

    @Override
    public int pop() {
       if (empty()){
           throw new UnsupportedOperationException("栈为空");
       }
       int data = this.elem[this.top-1];
       this.usedSize--;
       this.top--;
       return data;
    }

    @Override
    public int peek() {
        if (empty()){
            throw new UnsupportedOperationException("栈为空");
        }
        return this.elem[this.top-1];
    }

    @Override
    public boolean empty() {
        if (this.top == 0) {
            return true;
        }
        return false;
    }

    @Override
    public int size() {
        return this.usedSize;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_43765564/article/details/89608044