Java initially implements stack based on array

Without further ado, let's start with the code

stack

stack, first in last out

Socket interface

First, let's define a Socket interface and give several methods that the stack must have, namely:

  • Determine if the stack is empty isEmpty()
  • Get the number of elements in the stack getSize()
  • Push on the stack (Object object)
  • pop()
  • Get the top element of the stack top()
public interface Stack {

    /**
     * 判断栈是否为空
     * @return boolean
     * @return
     * 时间:2018年4月26日
     */
    public boolean isEmpty();

    /**
     * 返回栈中元素数量
     * @return int
     * @return
     * 时间:2018年4月26日
     */
    public int getSize();

    /**
     * 压栈
     * @return void
     * @param object
     * 时间:2018年4月26日
     * @throws Exception 
     */
    public void push(Object object) throws Exception;

    /**
     * 出栈
     * @return Object
     * @return
     * 时间:2018年4月26日
     * @throws Exception 
     */
    public Object pop() throws Exception;

    /**
     * 获取栈顶元素
     * @return Object
     * @return
     * 时间:2018年4月26日
     * @throws Exception 
     */
    public Object top() throws Exception;

}

ArraySocket implementation class

Then, we create the ArraySocket class, implement the Stack interface, and override the methods in the Stack interface.
Look at the code first

public class ArrayStack implements Stack {

    /**
     * 数组栈默认容量
     */
    private static final int CAPACITY = 1024;

    /**
     * 数组栈实际容量
     */
    private int capacity;

    /**
     * 数组栈
     */
    private Object[] s;

    /**
     * 栈顶指针,-1代表栈空
     */
    private int top = -1;

    /**
     * 无参构造,创建默认容量大小的数组栈
     */
    public ArrayStack() {
        this(CAPACITY);
    }

    /**
     * 创建指定容量的数组栈
     * 
     * @param capacity
     */
    public ArrayStack(int capacity) {
        this.capacity = capacity;
        s = new Object[capacity];
    }

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

    @Override
    public int getSize() {
        return top + 1;
    }

    @Override
    public void push(Object object) throws Exception {
        //判断容量是否达上限
        if (CAPACITY == top) {
            throw new Exception("沾溢出");
        }
        s[++top] = object;
    }

    @Override
    public Object pop() throws Exception {
        // 判断栈是否为空
        if (isEmpty()) {
            throw new Exception("栈空");
        }
        // 返回栈顶元素
        Object object = s[top];
        //移除栈顶元素,方便垃圾回收
        s[top--] = null;
        return object;
    }

    @Override
    public Object top() throws Exception {
        if (isEmpty()) {
            throw new Exception("栈空");
        }
        return s[top];
    }

}

Here, we will explain the implementation principle in turn.

  1. We are implementing a stack based on an array, so it is essential to define an array first s. Considering encapsulation, we make the array private. And ensure that when the stack object is created, the array in the stack object is allocated space.
  2. Static constant CAPACITY, default 1024. private variable capacity. When the parameterized constructor is called, capacityan array of size is created. When the no-argument constructor is called, CAPACITYan array of default size is created.
  3. Create the top pointer top of the stack, considering that the starting index of the array is 0, so top is -1
  4. Rewrite isEmpty()the method to directly judge the top pointer top of the stack. When top=-1, the stack must be empty
  5. Rewrite getSize()the method, whenever an element is pushed on the stack, top+1, so top+1 is the number of elements in the stack
  6. Rewrite push(Object object)the method, first judge whether the stack is full, according to whether top is equal to capacity; if the stack is not full, assign the element to s[top]and add the top pointer of the stack +1
  7. Rewrite pop(), first determine that the stack is empty; if the stack is not empty, it will s[top]return and set it to empty
  8. Rewrite top(), first judge that the stack is empty; if the stack is not empty, it will s[top]return

test class

public class Test {

    public static void main(String[] args) throws Exception {
        Stack stack = new ArrayStack();
        stack.push(6);
        stack.push(5);
        stack.push(5);
        stack.push(5);
        stack.push(5);
        System.out.println(stack.pop());;
        System.out.println(stack.top());;
        System.out.println(stack.getSize());;
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325526983&siteId=291194637
Recommended