class PriorityQueue {
constructor() {
this.items = [];
}
enqueue(data, priority) {
let queueElement = new this.QueueElement(data, priority);
if (this.isEmpty()) {
this.items.push(queueElement);
} else {
let added = false;
for (let i = 0; i < this.items.length; i++) {
if (queueElement.priority < this.items[i].priority) {
this.items.splice(i, 0, queueElement);
added = true;
break;
}
}
if (!added) {
this.items.push(queueElement);
}
}
}
// 나머지 메소드는 기본 큐와 같다
}
// 큐에 새로운 원소를 생성하기 위한 내부 클래스, 각각의 원소는 데이터와 우선순위를 가진다
PriorityQueue.prototype.QueueElement = class {
constructor(data, priority) {
this.data = data;
this.priority = priority;
}
};