数据结构之队列结构
队列结构的特点是:一端进入,从另一端出去,可以总结为“先进先出”。
利用数组实现队列结构:
function Queue(params) {
this.items = [];
}
// 1、进入队列
Queue.prototype.enQueue = function (element) {
this.items.push(element);
}
// 2、出队列
Queue.prototype.deQueue = function () {
return this.items.shift()
}
// 3、查看队列前的元素
Queue.prototype.peek = function () {
return this.items[0];
}
// 4、队列是否为空
Queue.prototype.isEmpty = function () {
return this.items.length === 0;
}
// 5、队列长度
Queue.prototype.size = function () {
return this.items.length;
}
// 6、toString方法
Queue.prototype.toString = function () {
let result = '';
for (let i = 0; i < this.items.length; i++) {
const element = this.items[i];
result += element + ''
}
//
return result;
}

浙公网安备 33010602011771号