介绍
队列是遵循FIFO(First In First Out,先进先出,也称为先来先服务)原则的一组有序的项。 队列在尾部添加新元素,并从顶部移除元素。最新添加的元素必须排在队列的末尾。
在现实中,最常见的队列就是排队,当然不允许插队的存在哦
分析
实现一个队列,首先需要一个存储队列的数据结构,我们可以使用数组。
function Queue() {
    var items = [];
}
基本方法
代码实现
function Queue() {
  var items = [];
  this.enqueue = function(data) {
    items.push(data);
  };
  this.dequeue = function() {
    return items.shift();
  };
  this.size = function() {
    return items.length;
  };
  this.isEmpty = function() {
    return items.length === 0;
  };
  this.head = function() {
    return items[0] || null;
  };
  this.tail = function() {
    let len = items.length;
    if (len > 0) {
      return items[len - 1];
    }
    return null;
  };
  this.clear = function() {
    items = [];
    return true;
  };
  this.show = function() {
    console.log(items.toString());
  };
}
