JAVA Data Structure-06 (1) The basic data structure realization of the stack (array realization, linked list realization)

JAVA data structure-06 stack

JAVA Data Structure-06 (2) The application of stack structure, the calculation of simple calculation expressions (infix expression)
JAVA Data Structure-06 (3) The application of stack structure, three kinds of expressions before suffix, infix expression to suffix expression Realization and Algorithm Ideas of Inverse Polish Calculator

  • The stack is an ordered list of first in, last out

  • Insert and delete elements in the stack limitation that only the linear form the same end of the linear form of a special table of linear. Permit insertion and deletion of one end, one end of the change, referred to as a stack , the other end is a fixed end , Called the bottom of the stack .

  • The basic application scenarios of the stack :

    • Subroutine call: Before jumping to the subroutine, the address of the next instruction will be stored on the stack, and the address will be taken out after the subroutine is executed to return to the original program.
    • Handling recursive calls: similar to subroutine calls, except that in addition to storing the address of the next instruction, data such as parameters and area variables are also stored on the stack
    • Expression conversion [infix expression to postfix expression] and evaluation
    • Traversal of binary tree
    • Depth-first traversal of graphs

1. Implementation of the basic data structure of the stack

1.1 Use an array to simulate a stack

Thinking analysis:

  • Using an array to simulate the stack requires a pointer to the top of the stack;
  • Define a top to point to the data at the top of the stack, initialized to -1, indicating that there is no data,
  • Stack: When data is added to the stack. top++; stack[top] = data;
  • 出栈: int value = stack[top]; top–;
class ArrayStack {
    
    
    private int maxSize;
    private int stack[];
    private int top = -1;

    public ArrayStack(int maxSize) {
    
    
        this.maxSize = maxSize;
        this.stack = new int[maxSize];
    }

    public boolean isFull() {
    
    
        return top == maxSize - 1;
    }

    public boolean isEmpty() {
    
    
        return top == -1;
    }

    public int getTop(){
    
    
        if(isEmpty()){
    
    
            throw new RuntimeException("栈空");
        }
        return stack[top];
    }

    public void push(int data){
    
    
        if(isFull()){
    
    
            System.out.println("栈满,无法压入数据");
            return;
        }
        top++;
        stack[top] = data;
    }

    public int pop(){
    
    
        if(isEmpty()){
    
    
            throw new RuntimeException("栈为空,无法取出数据");
        }
        int value = stack[top];
        top --;
        return value;
    }

    public void showStack(){
    
    
        if(isEmpty()){
    
    
            System.out.println("栈为空");
            return ;
        }
        for (int i = top; i >=0 ; i--) {
    
    
            System.out.printf("stack[%d] = %d \n",i,stack[i]);
        }
    }
}

1.2 Use a linked list to simulate a stack

  • Because the end node of the linked list needs to be traversed, it is more troublesome, so the head of the linked list is set to the top of the stack .
  • When inserting elements, insert from the head node, using the head insertion method, and the auxiliary head pointer head is required.
class LinkListStack{
    
    
    private Data head = new Data(-1);

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

    //头插法,head.next表示栈顶
    public void push(Data node){
    
    
        node.next = head.next;
        head.next = node;
    }
    public Data pop(){
    
    
        if(isEmpty()){
    
    
            throw new RuntimeException("栈空,无法取出数据");
        }
        Data data = head.next;
        head.next = data.next;
        return data;
    }
    public void showStack(){
    
    
        if(isEmpty()){
    
    
            System.out.println("栈空");
            return;
        }
        Data temp = head.next;
        while (temp !=null){
    
    
            System.out.println(temp);
            temp = temp.next;
        }
    }

}

class Data{
    
    
    public int data;
    public Data next;

    public Data(int data) {
    
    
        this.data = data;
    }

    @Override
    public String toString() {
    
    
        return "Data{" +
                "data=" + data +
                '}';
    }
}

Guess you like

Origin blog.csdn.net/weixin_44634197/article/details/108552077