Java - Stacks and Queues

stacks and queues

1. Stack (Stack)

1.1 Concept

Stack: A special linear list that allows insertion and removal of elements only at one fixed end. One end where data insertion and deletion are performed is called
the top of the stack, and the other end is called the bottom of the stack. The data elements in the stack follow the LIFO (Last In First Out) principle.
Push stack: The insertion operation of the stack is called push/push/push, and the pushed data is at the top of the stack.
Pop: The deletion of the stack is called pop. The output data is on the top of the stack.

1.2 The usefulness of stacks

(1) The order of stacking and popping
Example:
insert image description here(2) Convert an infix expression to a postfix expression (reverse Polish expression) (understand) The
first step: multiply and divide first, then add and subtract, and add parentheses to it.
Step 2: The operator is placed after the current color bracket.
Step 3: Remove all parentheses to get the postfix expression.

insert image description here(3) To be able to calculate the suffix expression (this is to be)
, the subscript i is traversed backwards, the number is pushed onto the stack, and the operator is popped off the stack, the first popped from the stack is placed on the right side of the operator, and then placed on the stack after the operation , it can be calculated sequentially.
insert image description here

1.3 The method of stack

The methods in the stack
insert image description here
stack:
insert image description here
1. Implemented by sequence table, that is, implemented by tail insertion + tail deletion.
2. Implemented by linked list, both head and tail can be used. (The singly linked list uses the head insertion + head deletion method (the tail insertion method cannot be used, it is too troublesome). Or use a doubly linked list.)
insert image description here

1.4 Related topics

Leetcode Questions:
150 Questions Reverse Polish Expression Evaluation

    public int evalRPN(String[] tokens) {
    
    
        Stack<Integer> stack = new Stack<>();

        for(int i = 0;i < tokens.length;i++) {
    
    
            String val = tokens[i];
            if(isOperation(val) == false) {
    
    
                //如果不是运算符
                stack.push(Integer.parseInt(val));
            }else {
    
    
                //到底啥运算符????
                int num2 = stack.pop();
                int num1 = stack.pop();
                switch(val) {
    
    
                    case "+":
                        stack.push(num1+num2);
                        break;
                    case "-":
                        stack.push(num1-num2);
                        break;
                    case "*":
                        stack.push(num1*num2);
                        break;
                    case "/":
                        stack.push(num1/num2);
                        break;
                }
            }
        }
        return stack.pop();
    }
    private boolean isOperation(String x) {
    
    
        if(x.equals("+") || x.equals("-") ||x.equals("*") ||x.equals("/")) {
    
    
            return true;
        }
        return false;
    }

Minimum stack of 155 questions
insert image description here

class MinStack {
    
    
    private Stack<Integer> stack;
    private Stack<Integer> minStack;

    public MinStack() {
    
    
        stack = new Stack<>();
        minStack = new Stack<>();
    }

    public void push(int val) {
    
    
        stack.push(val);
        if(!minStack.empty()) {
    
    
            int top = minStack.peek();
            //比较 小于等于的话 也要放进去
            if(val <= top) {
    
    
                minStack.push(val);
            }
        }else{
    
    
            minStack.push(val);
        }
    }

    public void pop() {
    
    
        int popVal = stack.pop();
        if(!minStack.empty()) {
    
    
            int top = minStack.peek();
            if(top == popVal) {
    
    
                minStack.pop();
            }
        }
    }

    public int top() {
    
    
        return stack.peek();
    }

    public int getMin() {
    
    
        return minStack.peek();
    }
}

20 Valid Parentheses for Questions
insert image description here

public boolean isValid(String s) {
    
    
    Stack<Character> stack = new Stack<>();

    for(int i = 0; i < s.length();i++) {
    
    
        char ch = s.charAt(i);
        if(ch == '(' || ch == '[' || ch == '{') {
    
    
            //如果是左括号 直接入栈
            stack.push(ch);
        }else{
    
    
            //遇到了右括号
            if(stack.empty()) {
    
    
                //右括号多
                System.out.println("右括号多");
                return false;
            }
            char top = stack.peek();//哪个左括号
            if(top == '{' && ch == '}' || top == '[' && ch == ']' || top == '(' && ch == ')') {
    
    
                stack.pop();
            }else{
    
    
                //左右括号不匹配
                System.out.println("左右括号不匹配");
                return false;
            }
        }
    }
    if(!stack.empty()) {
    
    
        //左括号多
        System.out.println("左括号多");
        return false;
    }
    return true;
}

Niu Ke: Push-in and pop-up sequence of JZ31 question stack
Niu Ke JZ31 question hyperlink
insert image description here

import java.util.*;

public class Solution {
    
    
    public boolean IsPopOrder(int [] pushA,int [] popA) {
    
    
        Stack<Integer> stack = new Stack<>();
        int j = 0;
        for(int i = 0; i < pushA.length;i++) {
    
    
            stack.push(pushA[i]);
            while (!stack.empty()&& j< popA.length && stack.peek() == popA[j]){
    
    
                stack.pop();
                j++;
            }
        }
        return stack.empty();
    }
}

2. Queue

2.1 Concept

Queue : A special linear table that only allows data insertion operations at one end and deletion data operations at the other end. The queue has a first-in-first-out (First In First Out) entry queue: the end of the insertion operation is called the tail of the queue (Tail/ Rear) out of the queue: the end of the deletion operation is called the head of the queue (Head/Front)
insert image description here

insert image description hereCommonly used: offer(), poll(), peek()
insert image description here

2.2 Double-ended queue (Deque)

A double-ended queue (deque) refers to a queue that allows both ends to be enqueued and dequeued. Deque is short for "double ended queue". That means that elements can be dequeued and entered from the head of the queue, and can also be dequeued and entered from the end of the queue.

About
insert image description here

2.3 Related topics

3. Implement a stack with a queue. OJ link
4. Implement queues with stacks. OJ link

Guess you like

Origin blog.csdn.net/qq_43398758/article/details/122515894