循环队列

#include "SqQueue.h"
#include <iostream>
using namespace std;

void main()
{
	SqQueue<int> a;
	a.InQueue(5);
	int elem;
	a.GetHead(elem);
	cout<<elem<<endl;
	a.InQueue(6);
	cout<<a.Length()<<endl;
	a.OutQueue(elem);
	cout<<elem<<endl;
	a.OutQueue(elem);
	cout<<elem<<endl;
}



#ifndef _SQQUEUE_H_
#define _SQQUEUE_H_

#include <iostream>
using namespace std;

const int DEFAULT_NUMBER=10;
enum StatusCode{UNDER_FLOW,OVER_FLOW,SUCCESS};

template<class ElemType>
class SqQueue
{
    protected:
        int front,rear;
        ElemType *elem;
        int maxSize;
        
        void Init(int size);
    public:
        SqQueue(int size=DEFAULT_NUMBER);
        virtual ~SqQueue();

        int Length()const;
        bool Empty()const;
        void Clear();

        StatusCode InQueue(const ElemType &e);
        StatusCode OutQueue(ElemType &e);
        StatusCode GetHead(ElemType &e)const;

        SqQueue(const SqQueue<ElemType> &copy);
        SqQueue<ElemType>& operator = (const SqQueue<ElemType> &copy);
        
};

template<class ElemType>
SqQueue<ElemType>::SqQueue(int size)
{
    maxSize=size;
    elem=NULL;
    Init(size);
}

template<class ElemType>
SqQueue<ElemType>::~SqQueue()
{
    Clear();
    
}

template<class ElemType>
void SqQueue<ElemType>::Init(int size)
{
    if(elem!=NULL)
        delete []elem;
    else
        elem=new ElemType[size];
    front=0;
    rear=0;
}

template<class ElemType>
void SqQueue<ElemType>::Clear()
{
    if(elem!=NULL)
        delete []elem;
    front=0;
    rear=0;
}

template<class ElemType>
StatusCode SqQueue<ElemType>::InQueue(const ElemType &e)
{
    if(front%maxSize==(rear+1)%maxSize)
        return OVER_FLOW;
    else
    {
        elem[rear%maxSize]=e;
        ++rear;
        return SUCCESS;
    }
}

template<class ElemType>
StatusCode SqQueue<ElemType>::OutQueue(ElemType &e)
{
    if(Empty())
        return UNDER_FLOW;
    else
    {
        
        e=elem[front%maxSize];
        front++;
        return SUCCESS;
    }
}

template<class ElemType>
StatusCode SqQueue<ElemType>::GetHead(ElemType &e)const
{
    if(Empty())
        return UNDER_FLOW;
    else
    {
        e=elem[front%maxSize];
        return SUCCESS;
    }
}    

template<class ElemType>
int SqQueue<ElemType>::Length()const
{
    return ((rear%maxSize)-(front%maxSize));
}

template<class ElemType>
bool SqQueue<ElemType>::Empty()const
{
    return (rear%maxSize)==(front%maxSize);
}

template<class ElemType>
SqQueue<ElemType>::SqQueue(const SqQueue<ElemType> &copy)
{
    Init(copy.maxSize);
    front=copy.front;
    rear=copy.rear;

    for(int curPosition=front;curPosition<=rear;curPosition++)
        elem[curPosition%maxSize]=copy.elem[curPosition%maxSize];
}

template<class ElemType>
SqQueue<ElemType>& SqQueue<ElemType>::operator = (const SqQueue<ElemType> &copy)
{
    if(&copy!=this)
    {
        if(elem!=NULL)
            delete []elem;
        Init(copy.maxSize);
        front=copy.front;
        rear=copy.rear;

        for(int curPosition=front;curPosition<=rear;curPosition++)
            elem[curPosition%maxSize]=copy.elem[curPosition%maxSize];
    }
    return *this;
}
#endif


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值