Data structure_2: stack

Stack

Write at the beginning

  • Linear structure, stack operations are a subset of array operations
  • In the first-in-last-out data structure (LIFO), elements can only be added from one end, and elements can only be taken out from one end. This end is called the top of the stack

The realization of the stack: multiplexing dynamic arrays, and constructing ArrayStack<E> by way of interfaces

  • Stack interface:

      public interface Stack<E> {
      
          /**
           * 获取栈内元素容量
           * @return
           */
          int getSize();
      
          /**
           * 栈空判断
           * @return
           */
          boolean isEmpty();
      
          /**
           * 入栈
           * @param e
           */
          void push(E e);
      
          /**
           * 出栈
           * @return
           */
          E pop();
      
          /**
           * 获取栈顶元素
           * @return
           */
          E peek();
      }
    
  • Interface implementation class: ArrayStack<E>

      public class ArrayStack<E> implements Stack<E> {
    
          private Array<E> array;
      
          public ArrayStack(int capacity) {
              array = new Array<>(capacity);
          }
      
          public ArrayStack() {
              array = new Array<>();
          }
      
          public int getCapacity() {
              return array.getCapacity();
          }
      
          @Override
          public int getSize() {
              return array.getSize();
          }
      
          @Override
          public boolean isEmpty() {
              return array.isEmpty();
          }
      
          @Override
          public void push(E e) {
              array.addLast(e);
          }
      
          @Override
          public E pop() {
      		// 等同于:array.remove(size - 1);
              return array.removeLast();
          }
      
          @Override
          public E peek() {
      		// 等同于:array.get(size - 1);
              return array.getLast();
          }
      
          @Override
          public String toString() {
              return "stack.ArrayStack{" +
                      "array=" + array +
                      '}';
          }
      }
    

application

  • Editor-Undo (Undo) operation: The Undo operation will always undo the previous operation of the current step, and the stack operation will be executed sequentially from the operation stack.
  • Operating system-system call stack: record the interruption point of the program nested instruction execution, and push the interruption point record into the stack (analogous sub-function execution point, repeat sub-function execution point into the stack operation until the lowest sub-function is called), The pop operation is the operation of returning the result of the bottom interruption point in turn.
  • Editor-Bracket matching, take Likou-T20 as an example:
    • Question:

        给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。
        有效字符串需满足:
        	左括号必须用相同类型的右括号闭合。
        	左括号必须以正确的顺序闭合。
        注意空字符串可被认为是有效字符串。
      
    • Solution: stack usage characteristics, the given character string scanning element, categories of left parenthesis character ( (, [, { ), performing stack operations for non left parenthesis character classes: taking the top element pairing logic to determine, traversing the pairing is successful and After the end, the stack is empty as a successful match.

        import java.util.Stack;
        class Solution {
            public boolean isValid(String s) {
                Stack<Character> stack = new Stack<>();
                for (int i = 0; i < s.length(); i ++) {
                    char c = s.charAt(i);
                    if (c == '(' || c =='[' || c == '{') {
                        stack.push(c);
                    } else {
                        if (stack.isEmpty()) {
                            return false;
                        }
                        if (c == ')' && stack.pop() != '(') {
                            return false;
                        }
                        if (c == ']' && stack.pop() != '[') {
                            return false;
                        }
                        if (c == '}' && stack.pop() != '{') {
                            return false;
                        }
                    }
                }
                return stack.isEmpty();
            }
        }
      

Guess you like

Origin blog.csdn.net/Nerver_77/article/details/89786542