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 } }