Algorithmic customs clearance village - how to implement a stack based on an array (or linked list)

1. Implement a stack based on an array.

  • Check if the stack is empty
  • top element
  • stack
  • pop out
  • Expansion (because it is an array, it must be expanded)

1. Define variables

An array and a pointer are required to point to the previous space of the top element of the stack (it can also point to the top element of the stack, depending on the requirements)

    private Object[] stack;
    private int top;

2. Initialization

    MyStack() {
    
    
        stack = new Object[0];
    }

3. Judgment is empty

   public boolean isEmpty() {
    
    
        return top == 0;
    }

4. Expansion

 public void expandCapacity(int size) {
    
    
        int length = stack.length;
        if (size>length){
    
    
            // 扩容两倍
            size = size*3/2+1;
            stack = Arrays.copyOf(stack,size);
        }
    }

5. The top element of the stack

    public T peek() {
    
    
        T t = null;
        if (top > 0) t = (T) stack[top - 1];
        return t;
    }

6. Add elements

    public void push(T t) {
    
    
        expandCapacity(top+1);
        stack[top] = t;
        top++;
    }

7. Pop

  public T pop() {
    
    
        T t = peek();
        if (top > 0) {
    
    
            stack[top - 1] = null;
            top--;
        }
        return t;
    }

1.7 General code

public class MyStack<T> {
    
    
    private Object[] stack;
    private int top;

    // 初始化
    MyStack() {
    
    
        stack = new Object[0];
    }

    // 判断为空
    public boolean isEmpty() {
    
    
        return top == 0;
    }

    // 栈顶元素
    public T peek() {
    
    
        T t = null;
        if (top > 0) t = (T) stack[top - 1];
        return t;
    }

    //  添加元素
    public void push(T t) {
    
    
        expandCapacity(top + 1);
        stack[top] = t;
        top++;
    }

    // 出栈
    public T pop() {
    
    
        T t = peek();
        if (top > 0) {
    
    
            stack[top - 1] = null;
            top--;
        }
        return t;
    }

    // 扩充容量
    public void expandCapacity(int size) {
    
    
        int length = stack.length;
        if (size > length) {
    
    
            // 扩容两倍
            size = size * 3 / 2 + 1;
            stack = Arrays.copyOf(stack, size);
        }
    }

}

2. Linked list represents stack

  • Linked listNode
  • Node head node
  • Judging that the stack is empty
  • the top element of the stack
  • pop out
  • stack

1. Node and head node

    class Node<T> {
    
    
        public T t;
        public Node next;
    }
    public Node<T> head;

2. Initialization

    ListStack() {
    
    
        head = null;
    }

3. Determine if the stack is empty

    public boolean isEmpty() {
    
    
        return head == null;
    }

4. The top element of the stack

public T peek() {
    
    
        if (head == null) {
    
    
            return null;
        }
        return head.t;
    }

2.5 Popping

public T pop() {
    
    
        if (head == null) {
    
    
            return null;
        }
        T t = head.t;
        head = head.next;
        return t;
    }

6. Push

public void push(T t) {
    
    
        if (t == null) {
    
    
            throw new NullPointerException("null");
        }
        if (head == null) {
    
    
            head = new Node<>();
            head.t = t;
            head.next = null;
        } else {
    
    
            Node<T> temp = head;
            head = new Node<>();
            head.t = t;
            head.next = temp;
        }
    }

7. General code

public class ListStack<T> {
    
    
    class Node<T> {
    
    
        public T t;
        public Node next;
    }

    public Node<T> head;

    ListStack() {
    
    
        head = null;
    }

    public void push(T t) {
    
    
        if (t == null) {
    
    
            throw new NullPointerException("null");
        }
        if (head == null) {
    
    
            head = new Node<>();
            head.t = t;
            head.next = null;
        } else {
    
    
            Node<T> temp = head;
            head = new Node<>();
            head.t = t;
            head.next = temp;
        }
    }


    public T pop() {
    
    
        if (head == null) {
    
    
            return null;
        }
        T t = head.t;
        head = head.next;
        return t;
    }

    public T peek() {
    
    
        if (head == null) {
    
    
            return null;
        }
        return head.t;
    }


    public boolean isEmpty() {
    
    
        return head == null;
    }
}

Guess you like

Origin blog.csdn.net/qq_52843958/article/details/131995398
Recommended