class priorityElement { constructor (elem, priority) { this.elem = elem this.priority = priority } } class priorityQueue { constructor () { this.items = [] } enqueue (elem, priority) { let priorityElem = new priorityElement(elem, priority) let flag = true if (!this.isEmpty()) { for (let i = 0; i < this.size(); i++) { if (priorityElem.priority < this.items[i].priority) { flag = false this.items.splice(i, 0, priorityElem) break; } } if (flag) this.items.push(priorityElem) }else { this.items.push(priorityElem) } } isEmpty () { return this.items.length <= 0 } size () { return this.items.length } } var person = new priorityQueue() person.enqueue('a', 10) person.enqueue('b', 8) person.enqueue('c', 3) person.enqueue('d', 9) person.enqueue('e', 4) person.enqueue('f', 2) person.enqueue('g', 5) person.enqueue('h', 11) console.log(person)
来源:https://www.cnblogs.com/JunLan/p/12334345.html