Queue class
1 enum Error_code { 2 succeed, 3 overflow, 4 underflow 5 }; 6 7 typedef int Queue_entry; // Now queue element is int 8 9 const int max_queue = 10; 10 11 class Queue { 12 public: 13 Queue(); 14 ~Queue(); 15 16 bool empty() const; 17 Error_code append(const Queue_entry & item); 18 Error_code serve(); 19 Error_code retrieve(Queue_entry & item) const; 20 int size() const; 21 22 private: 23 int count; 24 int front, rear; 25 Queue_entry entry[max_queue]; 26 }; 27 28 Queue::Queue() { 29 count = 0; 30 front = 0; 31 rear = max_queue - 1; 32 } 33 34 Queue::~Queue() { 35 36 } 37 38 bool Queue::empty() const { 39 if (count == 0) 40 return true; 41 return false; 42 } 43 44 Error_code Queue::append(const Queue_entry & item) { 45 if (count == max_queue) 46 return overflow; 47 count++; 48 rear = ((rear+1)==max_queue) ? 0 : (rear+1); 49 entry[rear] = item; 50 return succeed; 51 } 52 53 Error_code Queue::serve() { 54 if (count == 0) 55 return underflow; 56 count--; 57 front = ((front+1) == max_queue) ? 0 : (front+1); 58 return succeed; 59 } 60 61 Error_code Queue::retrieve(Queue_entry & item) const { 62 if (count <= 0) 63 return underflow; 64 item = entry[front]; 65 return succeed; 66 } 67 68 int Queue::size() const{ 69 return count; 70 }