C++循环顺序队列(一)

2015-11-21 01:42:02 · 作者: · 浏览: 6

顾名思义:采用顺序结构存放的队列称为顺序队列
循环顺序队列可以避免队列的假溢出现象的发生。如下图示,循环队列的几种特殊情况。
这里写图片描述vc+3vbHjPGJyIC8+DQq208HQ0a27t7XEudi8/DwvcD4NCjxwcmUgY2xhc3M9"brush:sql;"> front=(front+1)%queueSize;

下面是个人的循环队列操作工程文件:

///////////////////////////////////////////////////////////////////////// //sqQueue.h //////////////////////////////////////////////////////////////////////// #ifndef MYHEAD_H #define MYHEAD_H #include"myhead.h" #endif #include
    
      ////////////////////////////////////////////////////////////////////////////// //循环顺序队列数据结构C++类声明(基类) template 
     
       class SqQueue { public: void clear();//把循环顺序队置空 Status deQueue(ElemType & e);//出队列 Status enQueue(ElemType & e);//进队列 Status getFront(ElemType & e);//读循环顺序队列队头的元素 int getLength();//求循环顺序队中元素个数 bool isEmpty();//判断循环顺序队是否为空 bool isFull();//判断循环顺序队是否为满 SqQueue
      
        operator =(SqQueue
       
         rightQ);//重载赋值运算符的定义 void display(); void randSqueue(); //****************************下面为
        系统自动调用构造函数及析构函数声明*************************// SqQueue(int size=20);//构造函数 ~SqQueue();//析构函数 SqQueue(const SqQueue
        
          & otherQ);//拷贝初始化构造函数 protected: int rear; int front; int queueSize; ElemType *base; }; /////////////////////////////////////////////////////////////////////////////////////////////// //循环顺序队列数据结构C++类实现(基类) template 
         
           void SqQueue
          
           ::clear() { front=rear; } template 
           
             Status SqQueue
            
             ::deQueue(ElemType & e) { if(isEmpty()) return ERROR; e=base[front]; front=(front+1)%queueSize; return OK; } template 
             
               Status SqQueue
              
               ::enQueue(ElemType &e) { if(isFull()) return ERROR; base[rear]=e; rear=(rear+1)%queueSize; return OK; } template 
               
                 Status SqQueue
                
::getFront(ElemType & e) { if(isEmpty()) return ERROR; e=base[front] return OK; } template int SqQueue ::getLength() { return (rear-front+queueSize)%queueSize; } template bool SqQueue ::isEmpty() { return rear==front?true:false; } template bool SqQueue ::isFull() { return (rear+1)%queueSize==front?true:false; } ///////系统构造函数及析构函数的实现 template SqQueue ::SqQueue(int size) { base=new ElemType[size]; assert(base!=0); front=rear=0; queueSize=size; } template SqQueue ::~SqQueue() { delete []base; } template SqQueue ::SqQueue(const SqQueue & otherQ) { base=new ElemType[otherQ.queueSize]; assert(base!=0); queueSize=otherQ.queueSize; front=otherQ.front; rear=otherQ.rear; for (int i = front;i%queueSize!=rear) { base[i]=otherQ.base[i]; i=(i+1)%queueSize; } } template void SqQueue ::display() { int n=getLength(); cout< %3Cbase%5Bi%2Bfront%5D%3B%0A%20%20%20%20%7D%0A%20%20%20%20cout%3C%3Cendl%3B%0A%20%20%20%20cout%3C%3Csetw(6)%3C%3C%22%E2%86%91%22%3B%0A%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20n-1%3B%20i%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20cout%3C%3Csetw(6)%3C%3C%22%20%22%3B%0A%20%20%20%20%7D%0A%20%20%20%20cout%3C%3Csetw(6)%3C%3C%22%E2%86%91%22%3C%3Cendl%3B%0A%0A%20%20%20%20cout%3C%3Csetw(6)%3C%3C%22front%22%3B%0A%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20n-1%3B%20i%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20cout%3C%3Csetw(6)%3C%3C%22%20%22%3B%0A%20%20%20%20%7D%0A%20%20%20%20cout%3C%3Csetw(6)%3C%3C%22rear%22%3C%3Cendl%3B%0A%0A%7D%0A%0Atemplate%3Ctypename%20ElemType%3E void SqQueue ::randSqueue() { ElemType Elem[11]; sran