JS(ES6新语法)实现数据结构——双端队列
·
双端队列:
概念: 双端队列是一种把队列和栈结合的数据结构。可以从两端进行增减项。
常见应用:存储撤销操作。
可用方法:
- isEmpty() ; 判断双端队列是否为空。返回布尔值。
- clear(); 清空双端队列。
- size();返回双端队列的大小。
- addFront(element);在双端队列前添加一个项。
- addBack(element);在双端队列后面添加一项。
- removeFront();删除双端队列头部项。
- removeBack();删除双端队列尾部项。
- peekFront();查看双端队列头部项。
- peekBack();查看双端队列尾部项。
- toString();将队列项转成字符串。
实现:
//使用对象模拟双端队列
class Deque { //声明双端队列类
constructor() {
this.items = {}; //队列对象
this.lowestCount = 0; //头部项的下标值
this.count = 0; //队列长度计数器(注意:this.count位置上永远是无值的。队列不为空,它的前一位就为队列尾部项);
}
isEmpty() { //判断双端队列是否为空,返回布尔值
return (this.count - this.lowestCount) === 0;
}
clear() { //清空双端队列,无返回值
this.items = {};
this.lowestCount = 0;
this.count = 0;
}
size() { //返回双端队列的大小
return this.count - this.lowestCount;
}
addFront(element) { //在双端队列前添加一个项
if (this.lowestCount === this.count) { //队列为空的情况
this.items[this.count] = element;
this.count++;
return;
}
if (this.lowestCount === 0) {
for (let i = this.count; i > 0; i--) {
this.items[i] = this.items[i - 1];
}
this.items[0] = element;
this.count++;
} else {
this.items[this.lowestCount - 1] = element;
this.lowestCount--;
}
}
addBack(element) { //在双端队列后面添加一项
this.items[this.count] = element;
this.count++;
}
removeFront() { //删除双端队列头部项
if (this.lowestCount === this.count) {
return undefined;
}
let w = this.items[this.lowestCount];
delete this.items[this.lowestCount];
this.lowestCount++;
return w;
}
removeBack() { //删除双端队列尾部项
if (this.lowestCount === this.count) {
return undefined;
}
let w = this.items[this.count - 1];
delete this.items[this.count - 1];
this.count--;
return w;
}
peekFront() { //查看双端队列头部项
return this.items[this.lowestCount];
}
peekBack() { //查看双端队列尾部项
return this.items[this.count - 1];
}
toString() { //将队列项转成字符串
if (this.lowestCount === this.count) {
return '';
}
let str = this.items[this.lowestCount];
for (let i = this.lowestCount+1; i < this.count; i++) {
str = `${str},${this.items[i]}`;
}
return str;
}
}
双端队列的使用:
//双端队列的使用
let myDeque = new Deque();
console.log(myDeque.size()); //0
myDeque.addFront(1); //在头部添加元素
myDeque.addFront(2);
console.log(myDeque.size()); //2
console.log(myDeque.toString()); //2,1
console.log(myDeque.isEmpty()); //false
myDeque.addFront(6);
console.log(myDeque.size()); //3
console.log(myDeque.toString()); //6,2,1
myDeque.addBack(8);
myDeque.addBack(3);
console.log(myDeque.size()); //5
console.log(myDeque.toString()); //6,2,1,8,3
myDeque.removeFront();
console.log(myDeque.size()); //4
console.log(myDeque.toString()); //2,1,8,3
myDeque.removeBack();
console.log(myDeque.size()); //3
console.log(myDeque.toString()); //2,1,8
console.log(myDeque.peekFront()); //查看头项 //2
console.log(myDeque.peekBack()); //查看尾项 //8
myDeque.clear(); //清空
console.log(myDeque.isEmpty()); //true
📦 前端资源合集 | 持续更新
🟢 前端0到1【持续更新】→ https://pan.quark.cn/s/5df55ccff7c4
🔵 前端进阶【持续更新】→ https://pan.quark.cn/s/2dec1c87b3ec
🟣 前端2026最新【持续更新】→ https://pan.quark.cn/s/77c8fa94161c
🔴 AI最新学习资料 → https://pan.baidu.com/s/1P9X2Qk_Fby3rFNVGw_WKow?pwd=46XG 提取码:46XG
觉得有用就点个赞+收藏,关注我持续分享前端干货 💡
AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。
更多推荐


所有评论(0)