SqQueue(环状队列(顺序表结构))

  1 template<typename ElemType>
  2 class SqQueue
  3 {
  4 protected:
  5     int count;
  6     int front,rear;
  7     int maxSize;
  8     ElemType *elem;
  9 public:
 10     SqQueue(){}
 11     SqQueue(int size);
 12     virtual ~SqQueue();
 13     int Length() const;
 14     bool Empty() const;
 15     void Clear();
 16     void Traverse(void (*visit)(const ElemType &))const;
 17     bool OutQueue(ElemType &e);
 18     bool GetHead(ElemType &e) const;
 19     bool InQueue(const ElemType &e);
 20     SqQueue(const SqQueue<ElemType> &copy);
 21     SqQueue<ElemType> &operator =(const SqQueue<ElemType> &copy);
 22 };
 23 template<typename ElemType>
 24 //构造函数
 25 SqQueue<ElemType>::SqQueue(int size)
 26 {
 27     maxSize=size;
 28     elem=new ElemType[maxSize];
 29     rear=front=0;
 30     count=0;
 31 }
 32 template<typename ElemType>
 33 //虚虚构函数
 34 SqQueue<ElemType>::~SqQueue()
 35 {
 36     delete []elem;
 37 }
 38 template<typename ElemType>
 39 //SqQueue长度
 40 int SqQueue<ElemType>::Length() const
 41 {
 42     return count;
 43 }
 44 template<typename ElemType>
 45 //判断空队列
 46 bool SqQueue<ElemType>::Empty() const
 47 {
 48     return count==0;
 49 }
 50 template<typename ElemType>
 51 //清空队列
 52 void SqQueue<ElemType>:: Clear()
 53 {
 54     rear=front=0;
 55     count=0;
 56 }
 57 template<typename ElemType>
 58 //遍历队列
 59 void SqQueue<ElemType>::Traverse(void(*visit)(const ElemType & ))const
 60 {
 61     for(int pos=front;pos!=rear;pos=(pos+1)%maxSize)
 62         (*visit)(elem[pos]);
 63 
 64 }
 65 template<typename ElemType>
 66 //队首出
 67 bool SqQueue<ElemType>::OutQueue(ElemType &e)
 68 {
 69     if(!Empty())
 70     {
 71         e=elem[front];
 72         front=(front+1)%maxSize;
 73         count--;
 74         return true;
 75     }
 76     else return false;
 77 }
 78 template<typename ElemType>
 79 //加入新队尾
 80 bool SqQueue<ElemType>::InQueue(const ElemType &e)
 81 {
 82     if(count==maxSize)
 83         return false;
 84     else
 85     {
 86         elem[rear]=e;
 87         rear=(rear+1)%maxSize;
 88         count++;
 89         return true;
 90     }
 91 
 92 }
 93 template<typename ElemType>
 94 //复制构造
 95 SqQueue<ElemType>::SqQueue(const SqQueue<ElemType> &copy)
 96 {
 97     maxSize=copy.maxSize;
 98     elem=new ElemType[maxSize];
 99     front=copy.front;
100     rear=copy.rear;
101     count=copy.count;
102     for(int pos=front;pos!=rear;pos=(pos+1)%maxSize)
103         elem[pos]=copy.elem[pos];
104 }
105 template<typename ElemType>
106 //重载=operator
107 SqQueue<ElemType> &SqQueue<ElemType>::operator=(const SqQueue<ElemType> &copy)
108 {
109     if(&copy!=this)
110     {
111         maxSize=copy.maxSize;
112         delete []elem;
113         elem=new SqQueue<ElemType>[maxSize];
114         count=copy.count;
115         front=copy.front;
116         rear=copy.rear;
117         for(int pos=front;pos!=rear;pos=(pos+1)%maxSize)
118             elem[pos]=copy.elem[pos];
119         return *this;
120     }
121 
122 }

转载于:https://www.cnblogs.com/ljwTiey/p/4268139.html

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值