Queue Lecturer : Kritawan Siriboon, Room no. 913 Text : Data Structures & Algorithm Analysis in C, C++,… Mark Allen Weiss, Addison Wesley
Queues rear front tail head คือแถวคอยของของ ของในแถวมีลำดับ การเอาของออก(dequeue) ทำทางหัวคิว (front, head) การเอาของเข้า (enqueue) ทำทางท้ายคิว (rear, tail) rear tail front head FirstInFirstOut FIFO list
Data Structure Linked List Implementation rear front D C B A A is the first who enqueue,followed by B, C and D.
Data Structure Array Implementation straight array circular array C D E f r 0 1 2 3 4 1 E 4 = r C void increase(int &i) f = 2 D if (i == (MAX -1)) i = 0; else i++; 3
Data Structure/Operations f r 0 1 2 3 4 enqueue(‘F’) F dequeue() 0 1 2 3 4 0 1 2 3 4 char data[MAX]; int f,r; C D E C D E F r f r f f r A f,r 0 1 2 3 4 enqueue(i) i = dequeue() init() if (! full()) increase(r); data[r] = i; else ... if (! empty()) fi = data[f]; increase(f); return fi; else ... r = -1; f = 0; empty() full() ?
What condition makes queue empty() ? 0 1 2 3 4 D E f r 0 1 2 3 4 E f,r 0 1 2 3 4 r f 0 1 2 3 4 A B f r 0 1 2 3 4 B f,r 0 1 2 3 4 r f What condition makes queue full() ? Both empty & full C D E f r 0 1 2 3 4 A B x = r; increase(x) == f; deq() C D E f r 0 1 2 3 4 B C D E 0 1 2 3 4 F B r f enq(‘F’)
template<class T, int MAX = 5> class queue{ T data[MAX]; int front, rear; int count; }; enq deq init full empty count++ count-- count = 0 count == MAX count == 0