javaScript数据结构--队列
队列遵循FIFO(First In First Out, 先进先出)。常见的队列的例子就是排队

实现一个队列的数据结构:
function Queue() {
var items = [];
// 向队列的尾部添加一个或多个新的项
this.enqueue = function(element) {
items.push(element);
}
// 移除排在队列最前面的项
this.dequeue = function() {
return items.shift();
}
// 返回队列中的第一个元素
this.front = function() {
return items[0];
}
// 返回队列包含的元素
this.size = function() {
return items.length;
}
// 队列是否是空的
this.isEmpty = function() {
return items.length == 0 ;
}
this.clear = function() {
items = [];
}
this.print = function() {
console.log(items.toString());
}
}
var queue = new Queue();
queue.enqueue('a');
queue.enqueue('b');
console.log(queue.front()); // a
console.log(queue.size()); // 2
console.log(queue.isEmpty()); // false
queue.dequeue();
console.log(queue.front()); // b
console.log(queue.size()); // 1
console.log(queue.isEmpty()); // false