การดูคิวลำดับความสำคัญหมายถึงการรับค่าที่มีลำดับความสำคัญสูงสุดโดยไม่ต้องลบออก ดังนั้นเราสามารถใช้ฟังก์ชัน peek ได้ดังนี้ &minusl
ตัวอย่าง
peek() {
if (isEmpty()) {
console.log("Queue Underflow!");
return;
}
return this.container[this.container.length - 1];
} คุณสามารถตรวจสอบว่าฟังก์ชันนี้ทำงานได้ดีหรือไม่โดยใช้ -
ตัวอย่าง
let q = new PriorityQueue(4);
q.enqueue("Hello", 3);
q.enqueue("World", 2);
q.enqueue("Foo", 8);
console.log(q.peek());
q.display(); ผลลัพธ์
สิ่งนี้จะให้ผลลัพธ์ -
{ data: 'Foo', priority: 8 }
[ { data: 'World', priority: 2 },
{ data: 'Hello', priority: 3 },
{ data: 'Foo', priority: 8 } ] อย่างที่คุณเห็นที่นี่ peek() แตกต่างจาก dequeue โดยเพียงแค่คืนค่าด้านหน้าโดยไม่ต้องลบออก