• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

C++实现循环队列和链式队列的示例

c++ 搞代码 4年前 (2022-01-06) 23次浏览 已收录 0个评论

下面小编就为大家分享一篇C++实现循环队列和链式队列的示例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

循环队列:

1.循环队列中判断队空的方法是判断front==rear,队满的方法是判断front=(rear+1)%maxSize。(我曾经想过为什么不用一个length表示队长,当length==maxSize时队满)原因就是,在频繁的队列操作中,多出一个变量会大量的增加执行时间,所以不如浪费一个数组空间来得划算。

2.用单链表表示的链式队列特别适合于数据元素变动较大的情形,而且不存在溢出的情况。

 template class SeqQueue{ protected: T *element; int front,rear; int maxSize; public: SeqQueue(int sz=10){ front=rear=0; maxSize=sz; element=new T[maxSize]; } ~SeqQueue(){ delete[] element; } bool EnQueue(const T& x){//入队 if(isFull()) return false; element[rear]=x; rear=(rear+1)%maxSize; return true; } bool DeQueue(T& x){//出队 if(isEmpty()) return false; x=element[front]; front=(front+1)%maxSize; return true; } bool getFront(T& x){//获取队首元素 if(isEmpty()) return false; x=element[front]; return true; } void makeEmpty(){//队列置空 front=rear=0; } bool isEmpty()const{//判断队列是否为空 return (rear==front)?true:false; } bool isFull()const{//队列是否为满 return ((rear+1)%maxSize==front)?true:false; } int getSize()const{ return (rear-front+maxSize)%maxSize; } };

测试代码如下:

 void menu(){ cout<<"1.入队"<<endl; cout<<"2.获取队首元素"<<endl; cout<<"3.出队"<<endl; cout<<"4.队列置空"<<endl; cout<<"5.获取队中元素数量"<<endl; cout<<"6.退出"<<endl; } void function(int num,SeqQueue *sq){ switch(num){ int x; case 1: cin>>x; sq->EnQueue(x); break; case 2: sq->getFront(x); cout<<x<DeQueue(x); break; case 4: sq->makeEmpty(); break; case 5: x=sq->getSize(); cout<<x<<endl; break; default: exit(1); } } int main(int argc, char** argv) { SeqQueue *sq=new SeqQueue; int num; while(true){ menu(); cin>>num; function(num,sq); } delete sq; return 0; }

之后是链式队列,实现类代码和测试代码如下:

 #include  using namespace std; template struct LinkNode{ T data; LinkNode *link; LinkNode(T& x,LinkNode *<div style="color:transparent">来源gaodai.ma#com搞##代!^码网</div>l=NULL){ data=x; link=l; } }; template class LinkedQueue{ protected: LinkNode *front,*rear; public: LinkedQueue(){ front=rear=NULL; } ~LinkedQueue(){ makeEmpty(); } bool enQueue(T& x){ if(front==NULL) front=rear=new LinkNode(x); else{ rear=rear->link=new LinkNode(x); } return true; } bool deQueue(T& x){ if(isEmpty()) return false; LinkNode *p=front; x=front->data; front=front->link; delete p; return true; } bool getFront(T& x)const{ if(isEmpty()) return false; x=front->data; return true; } void makeEmpty(){ LinkNode *p; while(front!=NULL){ p=front; front=front->link; delete p; } } bool isEmpty()const{ return (front==NULL)?true:false; } int getSize()const{ LinkNode *p; int count=0; p=front; while(p!=NULL){ count++; p=p->link; } return count; } }; void menu(){ cout<<"1.入队"<<endl; cout<<"2.获取队首元素"<<endl; cout<<"3.出队"<<endl; cout<<"4.队列置空"<<endl; cout<<"5.获取队中元素数量"<<endl; cout<<"6.退出"<<endl; } void function(int num,LinkedQueue *lq){ switch(num){ int x; case 1: cin>>x; lq->enQueue(x); break; case 2: lq->getFront(x); cout<<x<deQueue(x); break; case 4: lq->makeEmpty(); break; case 5: x=lq->getSize(); cout<<x<<endl; break; default: exit(1); } } int main(int argc, char** argv) { LinkedQueue *lq=new LinkedQueue; int num; while(true){ menu(); cin>>num; function(num,lq); } delete lq; return 0; }

以上就是C++实现循环队列和链式队列的示例的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:C++实现循环队列和链式队列的示例
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址