Data structure: Stack and Queue
อันนี้จะเป็นเรื่อง Data structure นะคับ (ไม่ใช่ Data type) สำหรับ Stack and Queue ต่างกันยังไง มาลองทบทวนกันนะคับ
สำหรับ Stack จะเป็นแบบ LIFO (Last In-First Out) คือ เข้าทีหลัง ออกก่อนนะคับ คำสั่งที่เกี่ยวข้องคือ push, pop
อันนี้เป็นตัวอย่าง Using lists as Stacks นะคับ
มาต่อกันที่ Queue นะคับ ซึ่งจะเป็นแบบ FITO (First In-First Out) คือ แบบยุติธรรมเหมือนเราเข้าคิวเลย คือ เข้าก่อนออกก่อน มาทีหลังก้อออกทีหลัง คำสั่งที่เกี่ยวข้องคือ enqueue และ dequeue
Basic operations on a queue:
1. Enque/Add/Put: Insert a data item on the back or rear of the queue.
2. Deque/Delete/Get: Remove an item from the front of the queue.
3. Peek: Read the value of an item from the front of the queue without removing it.
มาดู code ตัวอย่างจากกันนะคับ สำหรับ queue เราต้อง import collection ในส่วนของ queue เข้ามาด้วยนะคับ ตามนี้
แล้วพบกันใหม่ตอนถัดไปครับ