Java code implements stack structure

stack structure

insert image description here

Stack (Stack) is a linear storage structure, it has the following characteristics

  • The data elements in the stack follow the principle of "last in, first out" (First In Last Out), referred to as FILO structure.
  • Insertion and deletion can only be performed at the top of the stack.

Related concepts

  • Stack top and stack bottom: The end that allows elements to be inserted and deleted is called the top of the stack, and the other end is called the bottom of the stack.
  • Push stack: The insertion operation of the stack is called push stack, also known as push stack and push stack.
  • Popping the stack: The deletion operation of the stack is also called popping the stack.

Application scenarios of the stack

  • subroutine call
  • Handle recursive calls
  • Conversion and Evaluation of Expressions
  • Binary tree traversal
  • A depth-first search method for graphs

Java code implements stack structure

// 利用数组实现一个栈结构
public class ArrayStack<T> {
    
    

    // 声明数组
    private T[] arr = (T[])new Object[1];
    // 栈元素初始数量
    private int num = 0;

    public ArrayStack(int size){
    
    
        this.arr = (T[])new Object[size];
    }

    // 入栈(压栈)
    public void push(T t){
    
    
        // 入栈前先判断当前栈容量是否有必要进行调整(主要是指扩容)
        addSize();
        arr[num++] = t;
    }

    // 出栈
    public T pop(){
    
    
        // 出栈时要先判断栈是否有元素
        if(isEmpty()) return null;
        T  popItem= arr[--num]; // 注意这里是--num
        arr[num] = null;        // 出栈时要将出栈的元素置空(剔除)
        // 每次pop后就判断一次是否达到缩容条件,达到条件就缩容节省空间
        subSize();
        return popItem;
    }

    // 返回栈中元素数量
    public int size(){
    
    
        return this.num;
    }

    // 判断栈是否为空
    public boolean isEmpty(){
    
    
        return num == 0;
    }

    // 扩容逻辑
    private void addSize(){
    
    
        // 如果当前栈中元素数量大于或者等于数组大小就进行2倍扩容
        if(num >= arr.length){
    
    
            resize(arr.length * 2);
        }
    }

    // 缩容逻辑
    private void subSize(){
    
    
        //当前栈中元素数量小于数组大小的一半就进行1/2缩容,节省空间开销
        if(num > 0 && num < arr.length / 2){
    
    
            resize(arr.length / 2);
        }
    }

    // 调整栈容量
    private void resize(int size){
    
    
        T[] newArr = (T[])new Object[size];
        for (int i = 0; i < num; i++) {
    
       // 注意:这里循环做元素迁移的条件需要根据栈内元素数量来所以是i < num,
            newArr[i] = arr[i];
        }
        arr = newArr;
    }

    // 测试
    public static void main(String[] args) {
    
    
        ArrayStack<String> arrayStack = new ArrayStack<>(3);
        arrayStack.push("a");
        arrayStack.push("b");
        arrayStack.push("c");
        arrayStack.push("d");
        System.out.println(arrayStack);
        System.out.println(arrayStack.pop());
        System.out.println(arrayStack.pop());
        System.out.println(arrayStack.pop());
        System.out.println(arrayStack.pop());
        System.out.println(arrayStack.pop());
    }
}

Guess you like

Origin blog.csdn.net/qq_40436854/article/details/120490497