1 of 17

Queue

อาจารย์สุวิชยะ รัตตะรมย์

สำนักวิชาเทคโนโลยีสารสนเทศและการสื่อสาร

2 of 17

หัวข้อวันนี้

  • ADT of Queue
    • Value Definition
    • Operation Definition
  • Implementations of Queue (static memory allocation)
    • Over flow , Under flow
    • function Enqueue , Dequeue

2

3 of 17

3

ลักษณะของคิวในชีวิตประจำวัน

4 of 17

ADT : Value Definition of Queue

  • เป็นโครงสร้างข้อมูลแบบรายการเชิงเส้น (Linear Data Structure) เหมือนกับสแตก
  • การใส่ข้อมูลเข้า (enqueue) กระทำที่ปลายด้านหนึ่งของคิว (rear)
  • การนำข้อมูลออก (dequeue) กระทำที่ปลายอีกด้านหนึ่งของคิว (front)
  • เข้าก่อนออกก่อน (First In First Out : FIFO)
    • ข้อมูลที่เข้าไปก่อนจะถูกนำออกมาใช้งานก่อน

4

5 of 17

ADT : Operation definition of Queue

  • การนำข้อมูลเข้า (enqueue)
    • นำข้อมูลเข้าได้ ณ ปลายด้านหนึ่งของคิว (rear) �(ถ้าจะเข้าคิวต้องต่อท้ายแถว)
  • การนำข้อมูลออก (dequeue)
    • นำข้อมูลออกจากปลายอีกด้านหนึ่งของคิว (front) (หัวแถวจะได้ออกจากคิวก่อน)

5

6 of 17

Implementation : Accessing Queue

  • การเข้าถึงข้อมูลในโครงสร้างคิวต้องอาศัย index 2 ตัว คือ
    • index ที่ชี้ไปยังข้อมูลตัวแรกของคิว (front)
    • index ที่ชี้ไปยังข้อมูลตัวสุดท้ายของคิว (rear)

6

A

B

C

empty

empty

empty

front

rear

ข้อมูลออก

ข้อมูลเข้า

7 of 17

Implementation : enqueue , dequeue

  • enqueue
    • เพิ่มค่าของ rear ไปอีก 1 ช่อง (กำหนดตำแหน่งท้ายคิวที่จะนำข้อมูลมาต่อ)
    • นำสมาชิกใหม่เข้ามาไว้ ณ ตำแหน่ง rear index
  • dequeue
    • นำข้อมูล ณ ตำแหน่ง front index ออกจากคิว
    • เพิ่มค่าของ front ไปอีก 1 ช่อง (ไปชี้ที่ข้อมูลหัวคิวตำแหน่งถัดไป)

7

8 of 17

8

D

C

B

A

Queue[4]

init(Queue)

enqueue(‘A’,Queue)

enqueue(‘B’,Queue)

X = dequeue(Queue)

enqueue(‘C’,Queue)

enqueue(‘D’,Queue)

Y = dequeue(Queue)

X = dequeue(Queue)

4

3

2

1

front = 0

1

1

2

2

2

3

4

rear = 0

1

2

2

3

4

4

4

9 of 17

Implementation of Queue �(static memory allocation)

9

10 of 17

  • การสร้างคิว คล้ายกับสแตก
    • ใช้อาร์เรย์ช่วยในการสร้างคิวได้
  • ในภาษาซี นิยมสร้างคิวโดยกำหนดเป็นตัวแปรแบบ structure ที่ประกอบไปด้วยเขตข้อมูล 3 เขต คือ
    • อาร์เรย์ที่ใช้เก็บข้อมูลในคิว
    • จำนวนเต็มที่ทำหน้าที่เป็น front index
    • จำนวนเต็มที่ทำหน้าที่เป็น rear index
  • ทุกครั้งที่มีการ enqueue, dequeue
    • ทำผ่านฟังก์ชัน

10

11 of 17

#define MAXQUEUE 10 //กำหนดขนาดของคิว

typedef struct

{

int data[MAXQUEUE]; // กำหนดเป็นคิวของ int โดยใช้ array

int front; // กำหนด front index

int rear; // กำหนด rear index

} QUEUE;

QUEUE Q;

11

ต้องกำหนดค่าเริ่มต้นให้กับคิว

12 of 17

pseudo code ฟังก์ชัน queue_init()

  • ในภาษาซี เนื่องจากข้อมูลตัวแรกของอาร์เรย์เริ่มที่ตำแหน่งที่ 0 ดังนั้น อาจกำหนดได้ดังนี้
    • คิวว่าง – front index มีค่า -1 , rear index มีค่า -1
    • คิวเต็ม – rear index มีค่า n-1

12

function queue_init ( queue )

front = -1 ;

rear = -1 ;

end function

13 of 17

ปัญหาในการ implement queue

  • underflow – เป็นปัญหาที่จะเกิดขึ้นเมื่อมีการดึงข้อมูล (dequeue) จากคิวที่ว่างอยู่
  • overflow – เป็นปัญหาที่จะเกิดขึ้นเมื่อมีการนำเข้าข้อมูล (enqueue) ลงในคิวที่มีข้อมูลอยู่เต็มแล้ว

13

14 of 17

ปัญหา underflow ในสแตกและคิว

14

สแตกที่สร้างโดยอาร์เรย์ ขนาด 4 ช่อง

front=-1

rear=-1

คิวสร้างโดยอาร์เรย์ ขนาด 4 ช่อง

top=-1

pop

dequeue

15 of 17

ปัญหา overflow ในสแตกและคิว

15

1

4

5

2

7

สแตกที่สร้างโดยอาร์เรย์ ขนาด 4 ช่อง

C

B

A

D

front

rear

คิวสร้างโดยอาร์เรย์ ขนาด 4 ช่อง

top

push

D

enqueue

16 of 17

pseudo code ฟังก์ชัน enqueue()

function enqueue ( data , queue )

// ตรวจสอบว่าคิวเต็มหรือไม่

if rear == MAXQUEUE-1 then

message( “QUEUE FULL!!!” )

else

rear = rear + 1

queue[rear] = data

// ถ้า insert ลงในคิวว่าง

if front == -1 then

front = 0

end if

end if

end function

16

empty

empty

empty

empty

front = rear = -1

17 of 17

pseudo code ฟังก์ชัน dequeue()

function dequeue ( queue )

// ถ้าคิวว่าง

if front == -1 then

message( “QUEUE EMPTY!!!” )

else

return queue[front]

// ถ้าในคิวมีข้อมูลเพียงตัวเดียว

if front == rear then

front = rear = -1

else

front = front + 1

end if

end if

end function

17

empty

A

empty

empty

front

rear