Javascript中的队列类
这是Queue类的完整实现-
示例
class Queue {
constructor(maxSize) {
//设置默认的最大大小(如果未提供)
if (isNaN(maxSize)) {
maxSize = 10;
}
this.maxSize = maxSize;
//初始化一个包含队列值的数组。
this.container = [];
}
//帮助程序功能在开发时显示所有值
display() {
console.log(this.container);
}
//检查队列是否为空
isEmpty() {
return this.container.length === 0;
}
//检查队列是否已满
isFull() {
return this.container.length >= this.maxSize;
}
enqueue(element) {
//检查队列是否已满
if (this.isFull()) {
console.log("队列溢出!"); return;
}
//由于我们要添加元素以结束,因此我们将其推送。
this.container.push(element);
}
dequeue() {
//检查是否为空
if (this.isEmpty()) {
console.log("队列下溢!");
return;
}
return this.container.shift();
}
peek() {
if (this.isEmpty()) {
console.log("队列下溢!");
return;
}
return this.container[0];
}
clear() {
this.container = [];
}
}热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短