Java programming: stack

A practical requirement of the stack

Please enter an expression.
Calculation formula: [7 2 2-5+1-5+3-3] Click to calculate [Figure below]
Insert picture description here
Excuse me: How does the bottom layer of the computer calculate the result? Note that it is not a simple list of calculations, because we look at this calculation, 7 * 2 * 2 - 5but how the computer understands this calculation (for the computer, it receives a string), we are discussing this problem. -> Stack

Introduction to the stack

  1. Stack in English is (stack)
  2. The stack is an 先入后出(FILO-First In Last Out)ordered list.
  3. A stack is a special linear table that restricts the insertion and deletion of elements in the linear table to the same end of the linear table. One end that allows insertion and deletion is yes, 变化的一端,称为栈顶(Top)and the other end is 固定的一端,称为栈底(Bottom).
  4. According to the definition of the stack, the first element placed in the stack is at the bottom of the stack, and the last element placed is at the top of the stack, while the deleted element is just the opposite. The last element is deleted first, and the first element is deleted last.
  5. Graphical way to illustrate pop and push
    Insert picture description here

Application scenarios of the stack

  1. Subroutine call: Before jumping to the subroutine, the address of the next instruction will be stored on the stack, and the address will be retrieved after the subroutine is executed to return to the original program.
  2. 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.
  3. Expression conversion [infix expression to postfix expression] and evaluation (actual solution).
  4. Traversal of the binary tree.
  5. Graphics depth-first (depth-first) search method.

Quick start of stack

  1. Use the array to simulate the use of the stack. Since the stack is an ordered list, of course, you can use the structure of the array to store the data content of the stack. Below we will use the array to simulate the stack's popping and stacking operations.
  2. Realize thinking analysis and draw a schematic diagram
    Insert picture description here
  3. Array simulation stack code implementation:
    package stack;
    
    import java.util.Scanner;
    
    public class ArrayStackDemo {
          
          
        public static void main(String[] args) {
          
          
            // 测试
            ArrayStack stack = new ArrayStack(4);
            String key = "";
            boolean loop = true;    // 控制是否退出
            Scanner scanner = new Scanner(System.in);
            while(loop){
          
          
                System.out.println("show:表示显示栈");
                System.out.println("exit:退出程序");
                System.out.println("push:表示添加数据到栈(入栈)");
                System.out.println("pop:表示从栈取出数据(出栈)");
                System.out.println("请输入操作名称:");
                key = scanner.next();
                switch (key){
          
          
                    case "show":
                        stack.list();
                        break;
                    case "push":
                        System.out.println("请输入数字:");
                        int value = scanner.nextInt();
                        stack.push(value);
                        break;
                    case "pop":
                        try {
          
          
                            System.out.printf("出栈的数据为:%d",stack.pop());
                        }catch (Exception e){
          
          
                            System.out.println(e.getMessage());
                        }
                        break;
                    case "exit":
                        scanner.close();
                        loop = false;
                        break;
                    default:
                        break;
                }
            }
            System.out.println("程序已退出");
        }
    }
    
    // 定义一个ArrayStack 表示栈
    class ArrayStack {
          
          
        private int maxSize;    // 栈的大小
        private int[] stack;    // 数组模拟栈,数据放在该数组
        private int top = -1;   // top表示栈顶,初始化为-1
    
        // 构造器
        public ArrayStack(int maxSize) {
          
          
            this.maxSize = maxSize;
            stack = new int[maxSize];
        }
    
        // 栈满
        public boolean isFull() {
          
          
            return top == maxSize - 1;
        }
    
        // 栈空
        public boolean isEmpty() {
          
          
            return top == -1;
        }
    
        // 入栈-push
        public void push(int value) {
          
          
            if (isFull()) {
          
          
                System.out.println("栈满");
                return;
            }
            stack[++top] = value;
        }
        // 出栈
        public int pop(){
          
          
            if(isEmpty()){
          
          
                // 抛出异常
                throw new RuntimeException("栈空,没有数据");
            }
            return stack[top--];
        }
    
        // 遍历
        public void list(){
          
          
            if(isEmpty()){
          
          
                System.out.println("栈空,没有数据");
                return;
            }
            for (int i = top; i >=0 ; i--) {
          
          
                System.out.printf("%d ",stack[i]);
            }
        }
    }
    

Guess you like

Origin blog.csdn.net/KaiSarH/article/details/108729909