同样使用数组来模拟队列
/*
Queue class
*/
function Queue() {
var items = [];
/*入队*/
this.enqueue = function (value) {
items.push(value);
}
/*出队,出列*/
this.dequeue = function () {
return items.shift();
}
/*返回队列的第一个元素*/
this.front = function () {
return items[0];
}
/*判断队列是否为空*/
this.isEmpty = function () {
return items.length == 0;
}
/*清空队列*/
this.clear = function () {
items = [];
}
/*判断队列长度*/
this.size = function () {
return items.length;
}
/*打印队列*/
this.print = function () {
console.log(items);
}
}