当前位置:首页 > JavaScript

js 堆栈实现

2026-03-01 04:16:44JavaScript

堆栈的基本概念

堆栈是一种遵循后进先出(LIFO)原则的数据结构。只允许在栈顶进行插入(push)和删除(pop)操作。堆栈可以通过数组或链表实现。

基于数组的实现

使用数组实现堆栈时,需要维护一个指针(通常称为top)来跟踪栈顶位置。

class Stack {
  constructor() {
    this.items = [];
    this.top = -1;
  }

  push(element) {
    this.top++;
    this.items[this.top] = element;
  }

  pop() {
    if (this.isEmpty()) return undefined;
    const poppedItem = this.items[this.top];
    this.top--;
    return poppedItem;
  }

  peek() {
    if (this.isEmpty()) return undefined;
    return this.items[this.top];
  }

  isEmpty() {
    return this.top === -1;
  }

  size() {
    return this.top + 1;
  }

  clear() {
    this.items = [];
    this.top = -1;
  }
}

基于链表的实现

链表实现堆栈时,每个节点包含数据和指向下一个节点的指针。栈顶是链表的头节点。

js 堆栈实现

class Node {
  constructor(data) {
    this.data = data;
    this.next = null;
  }
}

class LinkedListStack {
  constructor() {
    this.top = null;
    this.size = 0;
  }

  push(data) {
    const newNode = new Node(data);
    newNode.next = this.top;
    this.top = newNode;
    this.size++;
  }

  pop() {
    if (this.isEmpty()) return undefined;
    const poppedNode = this.top;
    this.top = this.top.next;
    this.size--;
    return poppedNode.data;
  }

  peek() {
    if (this.isEmpty()) return undefined;
    return this.top.data;
  }

  isEmpty() {
    return this.size === 0;
  }

  getSize() {
    return this.size;
  }

  clear() {
    this.top = null;
    this.size = 0;
  }
}

堆栈的应用场景

堆栈常用于需要回溯操作的场景,例如:

  • 函数调用栈
  • 浏览器历史记录
  • 撤销操作(Undo)
  • 括号匹配校验
  • 表达式求值

时间复杂度分析

堆栈操作的时间复杂度均为O(1):

js 堆栈实现

  • push:O(1)
  • pop:O(1)
  • peek:O(1)
  • isEmpty:O(1)

空间复杂度取决于存储的元素数量,为O(n)。

实际应用示例:括号匹配

使用堆栈检查表达式中的括号是否匹配:

function isBalanced(expression) {
  const stack = new Stack();
  const map = {
    '(': ')',
    '[': ']',
    '{': '}'
  };

  for (let char of expression) {
    if (char in map) {
      stack.push(char);
    } else if (char === ')' || char === ']' || char === '}') {
      if (stack.isEmpty() || map[stack.pop()] !== char) {
        return false;
      }
    }
  }

  return stack.isEmpty();
}

扩展实现:最小栈

设计一个能在O(1)时间内获取最小元素的栈:

class MinStack {
  constructor() {
    this.stack = [];
    this.minStack = [];
  }

  push(val) {
    this.stack.push(val);
    if (this.minStack.length === 0 || val <= this.minStack[this.minStack.length - 1]) {
      this.minStack.push(val);
    }
  }

  pop() {
    const val = this.stack.pop();
    if (val === this.minStack[this.minStack.length - 1]) {
      this.minStack.pop();
    }
    return val;
  }

  top() {
    return this.stack[this.stack.length - 1];
  }

  getMin() {
    return this.minStack[this.minStack.length - 1];
  }
}

标签: 堆栈js
分享给朋友:

相关文章

js实现打印

js实现打印

使用window.print()方法实现打印 在JavaScript中,可以通过调用window.print()方法直接触发浏览器的打印功能。这会打开系统的打印对话框,用户可以选择打印机和设置打印选项…

js实现拷贝

js实现拷贝

实现文本拷贝 使用 document.execCommand 方法(已废弃但兼容性较好): function copyText(text) { const textarea = document…

js实现变形

js实现变形

使用 CSS 和 JavaScript 实现元素变形 在 JavaScript 中实现变形效果通常结合 CSS 的 transform 属性,通过动态修改该属性实现元素的旋转、缩放、倾斜或平移。 基…

js实现祖玛

js实现祖玛

实现祖玛游戏的核心思路 祖玛游戏的核心玩法是发射彩色珠子,形成三个或以上相同颜色的珠子即可消除。以下是使用JavaScript实现的基本框架。 游戏初始化 创建画布并初始化游戏状态: const…

js实现图

js实现图

JavaScript 实现图的常用方法 在 JavaScript 中,图(Graph)可以通过多种方式实现,常见的包括邻接表(Adjacency List)和邻接矩阵(Adjacency Matrix…

js实现搜索

js实现搜索

实现搜索功能的方法 在JavaScript中实现搜索功能可以通过多种方式完成,以下是几种常见的实现方法。 使用数组的filter方法 通过数组的filter方法可以筛选出符合条件的数据项。假设有一个…