QueueQueue Lecturer : Kritawan Siriboon, Room no. 913 Text : Data Structures & Algorithm Analysis in C, C++,… Mark Allen Weiss, Addison Wesley
Queues คือแถวคอยของของ ของในแถวมีลำดับ การเอาของออก (dequeue) ทำทางหัวคิว (front, head) การเอาของเข้า (enqueue) ทำทางท้ายคิว (rear, tail) fronthead reartail FirstInFirstOut FIFO list
Data Structure Linked List Implementation rearfront DCBA 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 C D E 1 0 f = = r if (i == (MAX -1)) i = 0; else i++; void increase(int &i) MAX = size of array MAX = 5
Data Structure/Operations C D E f r const int MAX = 5; char data[MAX]; int f,r; if (! full()) increase(r); data[r] = i; else... if (! empty()) fi = data[f]; increase(f); return fi; else... enqueue(i) i = dequeue() init() C D E r enqueue(‘F’) dequeue() F C D E frf empty() full() ? r = -1; f = 0; A f,r r f F
What condition makes queue empty() ? D E f r E f,r rf A B f r B f,r rf C D E f r AB C D E F B r f C D E f r B deq() enq(‘F’) x = r; increase(x) == f; Both empty & full What condition makes queue full() ?
template class queue{ T data[MAX]; int front, rear; int count; }; enq deq init full empty count++ count-- count = 0 count == MAX count == 0