JS 队列(数据结构)- 笔记
【队列】代码:
/**
* 链表队列
*/
class LinkedListQueue {
/** @type {ListNode} */
#head;
/** @type {ListNode} */
#tail;
/** @type {number} */
#size;
constructor() {
this.#head = null;
this.#tail = null;
this.#size = 0;
}
/**
* 获取队列长度
* @returns {number}
*/
size() {
return this.#size;
}
/**
* 队列是否为空
* @returns {number}
*/
isEmpty() {
return this.#size === 0;
}
/**
* 入队
* @param {number} val
*/
enqueue(val) {
const node = new ListNode(val);
if (this.#head === null) {
this.#head = node;
this.#tail = node;
}
// 队列不为空,将节点添加到当前的尾节点之后
else {
this.#tail.next = node;
this.#tail = node;
}
this.#size += 1;
}
/**
* 获取队首元素
* @returns {number | null}
*/
peek() {
if (this.isEmpty()) {
return null;
}
return this.#head.val;
}
/**
* 出队
* @returns {number | null}
*/
dequeue() {
const first = this.peek();
if (first === null) {
return first;
}
this.#head = this.#head.next;
this.#size -= 1;
return first;
}
/**
* 转化为数组输出
* @returns {number[]}
*/
toArray() {
let node = this.#head;
const n = this.#size;
const arr = new Array(n);
for (let i = 0; i < n; i++) {
arr[i] = node.val;
node = node.next;
}
return arr;
}
}
/**
* 链表节点
*/
class ListNode {
/**
* @constructor
* @param {number} val
* @param {ListNode} next
*/
constructor(val, next = null) {
this.val = val;
this.next = next;
}
}
【双向队列】代码:
/**
* 双向队列(链表)
*/
class LinkedListDeque {
/** @type {ListNode} */
#head;
/** @type {ListNode} */
#tail;
/** @type {number} */
#size;
constructor() {
this.#head = null;
this.#tail = null;
this.#size = 0;
}
/**
* 获取队列长度
* @returns {number}
*/
size() {
return this.#size;
}
/**
* 队列是否为空
* @returns {boolean}
*/
isEmpty() {
return this.#size === 0;
}
/**
* 由队尾入队
* @param {number} val
*/
pushLast(val) {
const node = new ListNode(val);
if (this.#size === 0) {
this.#head = node;
this.#tail = node;
}
else {
this.#tail.next = node;
node.prev = this.#tail;
this.#tail = node;
}
this.#size += 1;
}
/**
* 有队首入队
* @param {number} val
*/
pushFirst(val) {
const node = new ListNode(val);
if (this.#size === 0) {
this.#head = node;
this.#tail = node;
}
else {
this.#head.prev = node;
node.next = this.#head;
this.#head = node;
}
this.#size += 1;
}
/**
* 由队尾出队
* @returns {number | null}
*/
popLast() {
if (this.#size === 0) {
return null;
}
const value = this.#tail.val;
const prev = this.#tail.prev;
if (prev !== null) {
prev.next = null;
this.#tail.prev = null;
}
this.#tail = prev;
this.#size -= 1;
return value;
}
/**
* 由队首出队
* @returns {number | null}
*/
popFirst() {
if (this.#size === 0) {
return null;
}
const value = this.#head.val;
const next = this.#head.next;
if (next !== null) {
next.prev = null;
this.#head.next = null;
}
this.#head = next;
this.#size -= 1;
return value;
}
/**
* 获取队尾元素
* @returns {number | null}
*/
peekLast() {
return this.#size === 0 ? null : this.#tail.val;
}
/**
* 获取队首元素
* @returns {number | null}
*/
peekFirst() {
return this.#size === 0 ? null : this.#head.val;
}
/**
* 转为数组进行输出
* @returns {number[]}
*/
toArray() {
const n = this.#size;
let node = this.#head;
const arr = new Array(n);
for (let i = 0; i < n; i++) {
arr[i] = node.val;
node = node.next;
}
return arr;
}
}
/**
* 双向链表节点
*/
class ListNode {
prev;
next;
val;
/**
* @constructor
* @param {number} val
*/
constructor(val) {
this.val = val;
this.prev = null;
this.next = null;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
2023-03-14 十二万九千六百年
2022-03-14 React.memo 与 React.useMemo 的区别
2022-03-14 JS 简易模拟Promise.all
2022-03-14 JS随即生成16进制颜色码的方法
2022-03-14 JS数组的洗牌方法