循环队列的数组表示函数的实现

#include < assert.h >
#include
< iostream.h >
#include
< stdlib.h >
#include
" Queue.h "
template
< class  T >
class  SeqQueue
{
public:
    SeqQueue(
int sz=10);
    
~SeqQueue(){delete[]elements;}
    
bool EnQueue(const T& x);
    
bool DeQueue(T& x);
    
bool getFront(T& x);
    
void makeEmpty(){front=rear=0;}
    
bool IsEmpty()const{return(front==rear)?true:false;}
    
bool IsFull()const{return((rear+1)%maxSize==front)?true:false;}
    friend ostream
& operator<<(ostream& os,SeqQueue<T>& Q);     //输出重载
protected:
    
int rear,front;              //队尾指针与对头指针
    T *elements;                 
    
int maxSize;                //队列最大可容纳个数
}
;

template
< class  T >
SeqQueue
< T > ::SeqQueue( int  sz):front( 0 ),rear( 0 ),maxSize(sz)
{
    elements
=new T[maxSize];
    assert(elements
!=NULL);
}
;

template
< class  T >
bool  SeqQueue < T > ::EnQueue( const  T &  x)
{
    
if(IsFull()==truereturn false;          //队列满,插入失败
    elements[rear]=x;                         //按照队尾指针指示插入位置
    rear=(rear+1)%maxSize;                    //队尾指针+1
    return true;
}
;

template
< class  T >
bool  SeqQueue < T > ::DeQueue(T &  x)
{
    
if(IsEmpty()==truereturn false;
    x
=elements[front];
    front
=(front+1)%maxSize;
    
return true;

}


template
< class  T >
bool  SeqQueue < T > ::getFront(T &  x)
{
    
if(IsEmpty()==truereturn false;
    x
=elements[front];
    
return true;
}
;

template
< class  T >
ostream
&   operator << (ostream &  os,SeqQueue < T >&  Q)
{
    os
<<"front="<<Q.front<<",rear="<<Q.rear<<endl;
    
for(int i=Q.front;i!=Q.rear;i=(i+1)%maxSize)
        os
<<i<<":"<<Q.elements[i]<<endl;
    
return os;
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值