JavaScript data structures and algorithms - stack

1. Initial stack structure

1.1 Features

Similar to the Tower of Hanoi 后进先出, only the top element of the stack can be operated at a time. Keywords: 压栈,退栈
Insert image description here
simple diagram:
Insert image description here

1.2 Precautions

When performing recursion, you need to set a termination condition. Otherwise, if you keep pushing things into the stack, it will cause栈溢出


2. Encapsulation of stack structure

2.1 Encapsulating a simple stack structure

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      class Stack {
    
    
        // 用数组模拟栈,但是需要给数组加一下限制,用#item避免外部访问
        #items = [];

        // 压栈
        push(data) {
    
    
          this.#items.push(data);
        }

        // 弹栈
        pop() {
    
    
          return this.#items.pop();
        }

        // 查看栈顶
        peek() {
    
    
          return this.#items[this.#items.length - 1];
        }

        // 查看当前栈是否为空
        isEmpty() {
    
    
          return this.#items.length == 0;
        }

        // 查看栈大小
        size() {
    
    
          return this.#items.length;
        }

        // 清空栈
        clear() {
    
    
          this.#items = [];
        }

        // 转为字符串
        toString() {
    
    
          return this.toString().toString(",");
        }
      }
      let stack = new Stack();
    </script>
  </body>
</html>

2.2 Convert decimal to binary using stack

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>

  <body>
    <script>
      class Stack {
    
    
        // 私有属性,避免被外部渲染
        #items = [];

        // 压栈
        push(data) {
    
    
          this.#items.push(data);
        }

        // 弹栈
        pop() {
    
    
          return this.#items.pop();
        }

        // 查看栈顶
        peek() {
    
    
          return this.#items[this.#items.length - 1];
        }

        // 查看当前栈是否为空
        isEmpty() {
    
    
          return this.#items.length == 0;
        }

        // 查看栈大小
        size() {
    
    
          return this.#items.length;
        }

        // 清空栈
        clear() {
    
    
          this.#items = [];
        }

        // 转为字符串
        toString() {
    
    
          return this.toString().toString(",");
        }
      }
      let stack = new Stack();

      // decNumber:要将那个数进行转换。base:将该数转换为几进制的数
      function convert(decNumber, base) {
    
    
        let number = decNumber;
        let remStack = new Stack();
        let string = "";
        let baseString = "0123456789ABCDEF";

        while (number > 0) {
    
    
          remStack.push(number % base);
          number = Math.floor(number / base);
        }
        while (!remStack.isEmpty()) {
    
    
          string += baseString[remStack.pop()];
        }

        return string;
      }
      convert(50);
    </script>
  </body>
</html>

Guess you like

Origin blog.csdn.net/qq_61402485/article/details/132107974