栈的概念

  • 栈是一种遵从后进先出原则的有序集合
  • 添加新元素的一端称为栈顶, 另一端称为栈底
  • 操作栈的元素时, 只能从栈顶操作(添加、益处或取值)

    数据结构与算法

  • 数据结构 + 算法 = 程序

  • 代码化繁为简
  • 提高代码性能

    栈的实现

  • push() 入栈方法

  • pop() 出栈方法
  • top() 获取栈顶值
  • size() 获取栈的元素个数
  • clear() 清空栈 ```javascript class Stack { constructor() { // 存储栈的数据 this.data = {} //记录站的个数 this.count = 0 }

    // 入栈方法 push(item) { this.data[this.count++] = item }

    // 出栈方法 pop() { if (this.isEmpty()) {

    1. return

    } const value = this.data[this.count - 1] delete this.data[—this.count] return value }

    // 获取栈顶值 top() { if (this.isEmpty()) {

    return
    

    } return this.data[this.count - 1] }

    // 获取栈的元素个数 size() { return this.count }

    // 清空栈 clear() { this.data = {} this.count = 0 }

    // 是否为空 isEmpty() { return this.count === 0 } }

const m = new Stack();

``` leecode练习题:
一、力扣
二 温度题、力扣