码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-02-02 17:53:10      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:

  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 }

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

标签:

原文地址:http://www.cnblogs.com/ljwTiey/p/4268139.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!