队列
队列是一种列表,不同的是队列只能在队尾插入元素,在队首删除元素。可以将队列想象成在银行前排队的人群,排在最前面的人第一个办理业务,新来的人只能在后面排队,直到轮到他们为止。
方法
push :向队尾插入元素
queue :在队首移除元素
peek :查看队首的元素
属性
dateStore : 使用数组存储
var Queue = function () {
this.dateStore = [];
}
Queue.prototype = {
//显示第一个元素
peek: function () {
if (this.dateStore.length > 0) {
return this.dateStore[0];
}
return false;
},
//读取队尾的元素
beek: function () {
return this.dateStore[this.dateStore.length - 1];
},
//清空
clear: function () {
this.dateStore = [];
},
//添加
push: function (element) {
this.dateStore[this.dateStore.length] = element;
},
//移除队首的元素
shift: function () {
return this.dateStore.splice(0, 1)[0];
},
//得到长度
getLenght: function () {
return this.dateStore.length;
},
empty: function () {
if (this.dateStore.length == 0) {
return true;
} else {
return false;
}
},
}