การทำงานกับคิวรี

โครงสร้างข้อมูลและอัลกอริทึมใน Python

Miriam Antona

Software Engineer

คิว

  • FIFO: First-In First-Out

    • สิ่งที่ ใส่เข้าไปก่อน จะถูก นำออกเป็นอันดับแรก

      ภาพแถวรอคิวในซูเปอร์มาร์เก็ตที่มีสามคน

โครงสร้างข้อมูลและอัลกอริทึมใน Python

คิว

  • FIFO: First-In First-Out

    • สิ่งที่ ใส่เข้าไปก่อน จะถูก นำออกเป็นอันดับแรก

      ภาพแถวรอคิวในซูเปอร์มาร์เก็ตที่มีสองคน เนื่องจากคนแรกออกจากคิวไปแล้ว

โครงสร้างข้อมูลและอัลกอริทึมใน Python

คิว

  • FIFO: First-In First-Out

    • สิ่งที่ ใส่เข้าไปก่อน จะถูก นำออกเป็นอันดับแรก

      ภาพแถวรอคิวในซูเปอร์มาร์เก็ตที่มีหนึ่งคน เนื่องจากคนที่หนึ่งและสองออกจากคิวไปแล้ว

โครงสร้างข้อมูลและอัลกอริทึมใน Python

คิว - โครงสร้าง

ภาพแผนผังโครงสร้างคิวที่มีชื่ออาหารนานาชาติ

โครงสร้างข้อมูลและอัลกอริทึมใน Python

คิว - โครงสร้าง

ภาพแผนผังโครงสร้างคิวที่มีชื่ออาหารนานาชาติ โดยคำว่า "head" ชี้ไปที่จุดเริ่มต้นของคิว

  • จุดเริ่มต้น: head
โครงสร้างข้อมูลและอัลกอริทึมใน Python

คิว - โครงสร้าง

ภาพแผนผังโครงสร้างคิวที่มีชื่ออาหารนานาชาติ โดยคำว่า "tail" ชี้ไปที่ส่วนท้ายของคิว

  • จุดเริ่มต้น: head
  • จุดสิ้นสุด: tail
โครงสร้างข้อมูลและอัลกอริทึมใน Python

คิว - คุณสมบัติ

ภาพแผนผังโครงสร้างคิวที่มีสองรายการ โดยคำว่า "head" ชี้ที่จุดเริ่มต้นและ "tail" ชี้ที่ส่วนท้าย

โครงสร้างข้อมูลและอัลกอริทึมใน Python

คิว - คุณสมบัติ

ภาพแผนผังโครงสร้างคิวที่มีการแทรกสมาชิกใหม่ที่ส่วนท้าย

  • แทรกได้เฉพาะที่ ส่วนท้าย เท่านั้น
    • Enqueue
โครงสร้างข้อมูลและอัลกอริทึมใน Python

คิว - คุณสมบัติ

ภาพแผนผังโครงสร้างคิวที่สมาชิกแรกถูกขีดทับเพื่อแสดงว่าจะถูกลบออก

  • แทรกได้เฉพาะที่ ส่วนท้าย เท่านั้น
    • Enqueue
  • ลบได้เฉพาะจาก head เท่านั้น
โครงสร้างข้อมูลและอัลกอริทึมใน Python

คิว - คุณสมบัติ

ภาพแผนผังโครงสร้างคิวที่เหลือสองรายการ เนื่องจากสมาชิกแรกถูกลบออกแล้ว

  • แทรกได้เฉพาะที่ ส่วนท้าย เท่านั้น
    • Enqueue
  • ลบได้เฉพาะจาก head เท่านั้น
    • Dequeue
  • คิวประเภทอื่น:
    • Doubly ended queues
    • Circular queues
    • Priority queues
โครงสร้างข้อมูลและอัลกอริทึมใน Python

คิว - กรณีการใช้งานในโลกจริง

  • งานพิมพ์ ในเครื่องพิมพ์
    • เอกสารจะถูกพิมพ์ตามลำดับที่ได้รับ
  • แอปพลิเคชันที่ ลำดับของคำขอมีความสำคัญ
    • การจองตั๋วคอนเสิร์ต
    • บริการแท็กซี่
โครงสร้างข้อมูลและอัลกอริทึมใน Python

คิว - การ implement

class Node:
  def __init__(self,data):
    self.data = data
    self.next = None
class Queue:
  def __init__(self):
    self.head = None
    self.tail = None
โครงสร้างข้อมูลและอัลกอริทึมใน Python

คิว - enqueue

def enqueue(self,data):

new_node = Node(data)
if self.head == None:

ภาพแผนผังโครงสร้างคิวที่มีหนึ่งสมาชิก ซึ่ง implement ด้วย node

โครงสร้างข้อมูลและอัลกอริทึมใน Python

คิว - enqueue

def enqueue(self,data):
  new_node = Node(data)
  if self.head == None:
    self.head = new_node
    self.tail = new_node

ภาพแผนผังโครงสร้างคิวที่มีหนึ่งสมาชิก ซึ่ง implement ด้วย node โดยคำว่า "head" และ "tail" ชี้ไปที่ node นั้น

โครงสร้างข้อมูลและอัลกอริทึมใน Python

คิว - enqueue

def enqueue(self,data):
  new_node = Node(data)
  if self.head == None:
    self.head = new_node
    self.tail = new_node

else:

ภาพแผนผังโครงสร้างคิวที่มีสอง node และกำลังเตรียม node ใหม่เพื่อแทรก

โครงสร้างข้อมูลและอัลกอริทึมใน Python

คิว - enqueue

def enqueue(self,data):
  new_node = Node(data)
  if self.head == None:
    self.head = new_node
    self.tail = new_node

else: self.tail.next = new_node

ภาพแผนผังโครงสร้างคิวที่มีสาม node โดยคำว่า "tail" ยังชี้ไปที่ node ที่สอง

โครงสร้างข้อมูลและอัลกอริทึมใน Python

คิว - enqueue

def enqueue(self,data):
  new_node = Node(data)
  if self.head == None:
    self.head = new_node
    self.tail = new_node

else: self.tail.next = new_node self.tail = new_node

ภาพแผนผังโครงสร้างคิวที่มีสาม node โดยคำว่า "tail" ชี้ไปที่ node ที่แทรกล่าสุด

โครงสร้างข้อมูลและอัลกอริทึมใน Python

คิว - dequeue

ภาพแผนผังโครงสร้างคิวที่มีสาม node โดยคำว่า "head" ชี้ไปที่ node แรก และ "tail" ชี้ไปที่ node สุดท้าย

def dequeue(self):

if self.head:
โครงสร้างข้อมูลและอัลกอริทึมใน Python

คิว - dequeue

ภาพแผนผังโครงสร้างคิวที่มีสาม node โดยคำว่า "head" และ "current_node" ชี้ไปที่ node แรก และ "tail" ชี้ไปที่ node สุดท้าย

def dequeue(self):
  if self.head:
    current_node = self.head





โครงสร้างข้อมูลและอัลกอริทึมใน Python

คิว - dequeue

ภาพแผนผังโครงสร้างคิวที่มีสาม node โดยคำว่า "head" ชี้ไปที่ node แรก "current_node" ชี้ไปที่ node ที่สอง และ "tail" ชี้ไปที่ node สุดท้าย

def dequeue(self):
  if self.head:
    current_node = self.head
    self.head = current_node.next




โครงสร้างข้อมูลและอัลกอริทึมใน Python

คิว - dequeue

ภาพแผนผังโครงสร้างคิวที่เหลือสอง node โดยคำว่า "head" ชี้ที่ node แรก และ "tail" ชี้ที่ node สุดท้าย มี node อีกตัวอยู่นอกคิวโดยมีคำว่า "current_node" ชี้ไปที่มัน

def dequeue(self):
  if self.head:
    current_node = self.head
    self.head = current_node.next
    current_node.next = None




โครงสร้างข้อมูลและอัลกอริทึมใน Python

คิว - dequeue

ภาพ node ที่มีคำว่า "current_node" และ "tail" ชี้ไปที่มัน โดยคำว่า "head" ชี้ไปที่ null

def dequeue(self):
  if self.head:
    current_node = self.head
    self.head = current_node.next
    current_node.next = None

if self.head == None:
โครงสร้างข้อมูลและอัลกอริทึมใน Python

คิว - dequeue

ภาพ node ที่มีคำว่า "current_node" ชี้ไปที่มัน โดยคำว่า "head" และ "tail" ชี้ไปที่ null

def dequeue(self):
  if self.head:
    current_node = self.head
    self.head = current_node.next
    current_node.next = None

    if self.head == None:
      self.tail = None    
โครงสร้างข้อมูลและอัลกอริทึมใน Python

SimpleQueue ใน Python

  • โมดูล: queue
    • Queue
    • SimpleQueue
import queue


orders_queue = queue.SimpleQueue()
orders_queue.put("Sushi") orders_queue.put("Lasagna") orders_queue.put("Paella")
print("The size is: ", orders_queue.qsize())
The size is: 3
print(orders_queue.get())
print(orders_queue.get())
print(orders_queue.get())
Sushi
Lasagna
Paella
print("Empty queue: ", orders_queue.empty())
Empty queue: True
โครงสร้างข้อมูลและอัลกอริทึมใน Python

มาฝึกกันเถอะ!

โครงสร้างข้อมูลและอัลกอริทึมใน Python

Preparing Video For Download...