栈和队列可通过JavaScript数组或自定义类实现。1. 栈遵循后进先出(LIFO),用push/pop操作实现高效入栈出栈;2. 队列遵循先进先出(FIFO),可用push/shift操作,但shift为O(n)影响性能;3. 可通过类封装实现peek、front、isEmpty等方法;4. 栈适用于递归模拟、表达式求值,队列适合任务调度、BFS等场景;5. 高性能需求时建议用对象+指针或链表优化队列实现。

JavaScript 中虽然没有内置的栈和队列类型,但我们可以利用数组或自定义类来实现这两种常用的数据结构。它们在算法设计、函数调用管理、任务调度等场景中非常有用。下面分别介绍栈和队列的基本原理与实现方式。
栈是一种遵循“后进先出”(LIFO, Last In First Out)原则的数据结构。常见的操作包括入栈(push)、出栈(pop)、查看栈顶元素(peek)和判断是否为空。
使用 JavaScript 数组可以轻松模拟栈行为:
以下是基于类的栈实现:
立即学习“Java免费学习笔记(深入)”;
class Stack {
  constructor() {
    this.items = [];
  }
<p>push(element) {
this.items.push(element);
}</p><p>pop() {
if (this.isEmpty()) return undefined;
return this.items.pop();
}</p><p>peek() {
if (this.isEmpty()) return undefined;
return this.items[this.items.length - 1];
}</p><p>isEmpty() {
return this.items.length === 0;
}</p><p>size() {
return this.items.length;
}
}</p>示例使用:
const stack = new Stack(); stack.push(1); stack.push(2); console.log(stack.peek()); // 2 console.log(stack.pop()); // 2 console.log(stack.size()); // 1
队列遵循“先进先出”(FIFO, First In First Out)原则。常用于任务排队、广度优先搜索等场景。基本操作包括入队(enqueue)、出队(dequeue)、查看队首元素(front)和判断是否为空。
虽然可以用数组的 push 和 shift 实现,但 shift 操作的时间复杂度为 O(n),效率较低。下面是一个简单但实用的队列类:
class Queue {
  constructor() {
    this.items = [];
  }
<p>enqueue(element) {
this.items.push(element);
}</p><p>dequeue() {
if (this.isEmpty()) return undefined;
return this.items.shift(); // 注意:shift 是 O(n)
}</p><p>front() {
if (this.isEmpty()) return undefined;
return this.items[0];
}</p><p>isEmpty() {
return this.items.length === 0;
}</p><p>size() {
return this.items.length;
}
}</p>示例使用:
const queue = new Queue();
queue.enqueue('a');
queue.enqueue('b');
console.log(queue.front());   // 'a'
console.log(queue.dequeue()); // 'a'
console.log(queue.size());    // 1
若需更高性能,可考虑使用双指针或链表实现,避免频繁的元素移动。
在实际开发中,选择合适的数据结构能显著提升代码效率和可读性。
基本上就这些。掌握栈和队列的手动实现,有助于深入理解 JavaScript 的数据操作机制,也能在不依赖外部库的情况下快速构建逻辑清晰的程序结构。
以上就是JavaScript中的数据结构实现:栈与队列的详细内容,更多请关注php中文网其它相关文章!
 
                        
                        每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
 
                 
                                
                                 收藏
收藏
                                                                             
                                
                                 收藏
收藏
                                                                             
                                
                                 收藏
收藏
                                                                            Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号