简单数据结构的实现之循环队列

#include <iostream>

using namespace std;

const int MAXQSIZE=100;

#ifndef SQQUEUE_H_INCLUDED
#define SQQUEUE_H_INCLUDED

template <class ElemType>
class SqQueue
{
private:
    ElemType* base;
    int front;
    int rear;
public:
    SqQueue();
    ~SqQueue();
    ElemType* Base(){return base;};
    int Front(){return front;}
    int Rear(){return rear;}
    void ClearQueue(){front=rear=0;}
    bool QueueEmpty(){return front==rear;}
    int QueueLength(){return (rear-front+MAXQSIZE)%MAXQSIZE;}
    bool GetHead(ElemType& e);
    bool EnQueue(ElemType e);
    bool DeQueue(ElemType& e);
};
template <class ElemType>
SqQueue<ElemType>::SqQueue()
{
    base=new ElemType[MAXQSIZE];
    front=rear=0;
}
template <class ElemType>
SqQueue<ElemType>::~SqQueue()
{
    delete []base;
    front=rear=0;
}
template <class ElemType>
bool SqQueue<ElemType>::GetHead(ElemType& e)
{
    if(front==rear)
    {
        return false;
    }
    else
    {
        e=base[front];
        return true;
    }
}
template <class ElemType>
bool SqQueue<ElemType>::EnQueue(ElemType e)
{
    if((rear+1)%MAXQSIZE==front)
    {
        return false;
    }
    else
    {
        base[rear]=e;
        rear=(rear+1)%MAXQSIZE;
        return true;
    }
}
template <class ElemType>
bool SqQueue<ElemType>::DeQueue(ElemType& e)
{
    if(front==rear)
    {
        return false;
    }
    else
    {
        e=base[front];
        front=(front+1)%MAXQSIZE;
        return true;
    }
}

#endif // SQQUEUE_H_INCLUDED

你可能感兴趣的:(简单数据结构的实现之循环队列)