www.pudn.com > GaitRsystem.rar > queue.h


// file queue.h 
// formual based queue 
 
#ifndef Queue_ 
#define Queue_ 
 
#include  
#include  
#include "xcept.h" 
 
template 
class Queue { 
// FIFO objects 
   public: 
      Queue(int MaxQueueSize = 1000); 
      ~Queue() {delete [] queue; queue = NULL;} 
      bool IsEmpty() const {return front == rear;} 
      bool IsFull() const {return ( 
           ((rear + 1) % MaxSize == front) ? 1 : 0);} 
      T First() const; // return front element 
      T Last() const; // return last element 
      Queue& Add(const T& x); 
      Queue& Delete(T& x); 
   private: 
      int front;   // one counterclockwise from first 
      int rear;    // last element 
      int MaxSize; // size of array queue 
      T *queue;    // element array 
}; 
 
template 
Queue::Queue(int MaxQueueSize) 
{// Create an empty queue whose capacity 
 // is MaxQueueSize. 
   MaxSize = MaxQueueSize + 1; 
   queue = new T[MaxSize]; 
   front = rear = 0; 
} 
 
template 
T Queue::First() const 
{// Return first element of queue.  Throw 
 // OutOfBounds exception if the queue is empty. 
   if (IsEmpty()) throw OutOfBounds(); 
   return queue[(front + 1) % MaxSize]; 
} 
 
template 
T Queue::Last() const 
{// Return last element of queue.  Throw 
 // OutOfBounds exception if the queue is empty. 
   if (IsEmpty()) throw OutOfBounds(); 
   return queue[rear]; 
} 
 
template 
Queue& Queue::Add(const T& x) 
{// Add x to the rear of the queue.  Throw 
 // NoMem exception if the queue is full. 
   if (IsFull()) throw NoMem(); 
   rear = (rear + 1) % MaxSize; 
   queue[rear] = x; 
   return *this; 
} 
 
template 
Queue& Queue::Delete(T& x) 
{// Delete first element and put in x.  Throw 
 // OutOfBounds exception if the queue is empty. 
   if (IsEmpty()) throw OutOfBounds(); 
   front = (front + 1) % MaxSize; 
   x = queue[front]; 
   return *this; 
} 
 
#endif