数据结构基础——栈

218 阅读1分钟

栈的特点

  • 先进后出

JS中使用数组模拟栈

// 栈——数据结构
// 在JS中使用数组来模拟栈
const stack = [];
// 入栈使用push
stack.push(1);   // 1比2先入栈
stack.push(2);
// 出栈使用pop()
const item1 = stack.pop();
const item2 = stack.pop();

栈的应用场景

  • 函数调用栈:最后被调用的函数反而最先被执行。
  • LeetCode:有效的括号,栈空说明是有效的括号。