一、队列
function Queue() {
this.container = [];
}
Queue.prototype = {
constructor: Queue,
enter: function enter(element) {
this.container.push(element);
},
leave: function leave() {
if (this.container.length === 0) return;
return this.container.shift();
},
size: function size() {
return this.container.length;
},
value: function value() {
return JSON.parse(JSON.stringify(this.container));
}
};
二、优先级队列
function Queue() {
this.container = [];
}
Queue.prototype = {
constructor: Queue,
enter: function enter(element, priority = 0) {
let obj = {
value: element,
priority: priority
};
if (priority === 0) {
this.container.push(obj);
return;
}
let flag = false;
for (let i = this.container.length - 1; i >= 0; i--) {
let item = this.container[i];
if (item.priority >= priority) {
this.container.splice(i + 1, 0, obj);
flag = true;
break;
}
};
!flag ? this.container.unshift(obj) : null;
},
leave: function leave() {
if (this.container.length === 0) return;
return this.container.shift();
},
size: function size() {
return this.container.length;
},
value: function value() {
return JSON.parse(JSON.stringify(this.container));
}
};