Peek คิวหมายถึงดูค่าที่หัวคิว (head) โดยไม่ลบออก ใช้งานได้ดังนี้:
ตัวอย่าง
peek() {
if (isEmpty()) {
console.log("Queue Underflow!");
return;
}
return this.container[0];
}
ทดสอบการทำงาน:
ตัวอย่าง
let q = new Queue(2); q.enqueue(3); q.enqueue(4); console.log(q.peek()); q.display();
ผลลัพธ์
จะได้ผลลัพธ์:
3 [ 3, 4 ]
peek() ต่างจาก dequeue ที่แค่คืนค่าไม่ได้ลบออก