C++ 循环队列基本算法实现

C++ 循环队列基本算法实现

#ifndef CircleQueue_h
#define CircleQueue_h

const int QueueSize = 1000;
template <class T>
class CircleQueue
{
public:
    CircleQueue(){front = rear = 0;}
    void EnQueue(T x);
    T DeQueue();
    T GetFront();
    void SetNull();
    int GetLength();
    bool Empty(){return front == rear ? true : false;}
private:
    T data[QueueSize];
    int front;
    int rear;
};
template <class T>
void CircleQueue<T>::EnQueue(T x){
    if((rear+1)%QueueSize == front) throw "overflow";
    rear = (rear + 1)%QueueSize; //队尾指向下一个位置
    data[rear] = x;
}
template <class T>
T CircleQueue<T>::DeQueue(){
    if(front == rear) throw "the Queue is null";
    front = (front+1)%QueueSize;
    T x = data[front];
    return x;
}
template <class T>
T CircleQueue<T>::GetFront(){
    if(front == rear) throw "the Queue is null";
    return data[(front +1)%QueueSize];
}
template <class T>
int CircleQueue<T>::GetLength(){
    return (rear-front +QueueSize)%QueueSize;
}
template <class T>
void CircleQueue<T>::SetNull(){
    rear = 0;
    front = 0;
}

#endif /* CircleQueue_h */

猜你喜欢

转载自www.cnblogs.com/ycbeginner/p/10006413.html
今日推荐