网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

C++中怎么实现一个循环队列-创新互联

本篇文章给大家分享的是有关C++中怎么实现一个循环队列,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

创新互联公司主营富宁网站建设的网络公司,主营网站建设方案,App定制开发,富宁h5微信小程序定制开发搭建,富宁网站营销推广欢迎富宁等地区企业咨询

具体内容如下

circularQueue.h

#pragma once#pragma once#ifndef CIRCULARQUEUE_H#define CIRCULARQUEUE_H #include#includeusing std::cout;using std::cin;using std::endl;using std::ostream;template class cirQueue; templateclass cirQueue{public: cirQueue(int sz); ~cirQueue(); void push(const T& elem);//进队 void pop(T& elem);//出队 bool empty();//查看队列是否为空 int getSize();//返回队列中元素的个数 void clearQueue();//清空队列中的元素 void print();//打印队列中的元素 int getfront() { return front; } int getrear() { return rear; } bool getTop(T& elem);//读取队列首个元素  template friend ostream& operator<<(ostream& os, cirQueue& queue); private: bool _full()const;//判断队列是否已满 int maxsize;//队列较大的空间 T* element;//存放于队列中的元素数组 int front;//模拟队头指针 int rear;//模拟队尾指针};  templatecirQueue::cirQueue(int sz) { maxsize = sz; element = new T[maxsize]; if (element == nullptr) cout << "内存分配失败" << endl; front = 0; rear = 0;}  templatecirQueue::~cirQueue() { if (element != nullptr) delete element;} //进队templatevoid cirQueue::push(const T& elem) {//需要保证队尾指针位置与首个元素相差一个位置 if (rear > (maxsize - 1)) rear -= maxsize ; if (front > (maxsize - 1)) front -= maxsize ; if (!_full()) {//队列未满的情况  element[rear++] = elem;//队尾向后移动一位 //++rear; } else { cout << "队列已满,不能插入!" << endl; return; }} //出队templatevoid cirQueue::pop(T& elem) { if (rear > (maxsize - 1)) rear -= (maxsize - 1); if (front > (maxsize - 1)) front -= (maxsize - 1); if (!empty()) {//队列未空的情况 elem = element[front++];//队头向后移动一位 element[front - 1] = 0;//置零 } else { cout << "队列已空!" << endl; return; }} //查看队列是否为空templatebool cirQueue::empty() { if (front == rear)//待定 return true; return false;} //返回队列中元素的个数templateint cirQueue::getSize() { int num = 0; if (front <= rear) return rear - front; else return maxsize - front + rear + 1;} //清空队列中的元素templatevoid cirQueue::clearQueue() { if (!empty()) { int Index = 0; while (front < rear) {//front逼近rear  element[front++] = 0;  if (front == rear)  return; } if (rear < front) {  while (front <= maxsize - 1)//删除front至数组尾端的数据  element[front++] = 0;  front -= maxsize;  while (front < rear) {//删除front至rear的数据  element[front++] = 0;  if (front == rear)   return;  } } }} //打印队列中的元素templatevoid cirQueue::print() {//与clearQueue函数原理一致,将front替换为Index if (!empty()) { int Index = front; while (Index < rear) {  cout << element[Index++] << " ";  if (Index == rear) {  cout << endl;  return;  } } if (rear < Index) {  while (Index <= maxsize - 1)  cout << element[Index++] << " ";  Index -= maxsize;  while (Index < rear) {  cout << element[Index++] << " ";  if (Index == rear) {   cout << endl;   return;  }  } } }} //读取队列首个元素templatebool cirQueue::getTop(T& elem) { if (!empty()) { elem = element[front]; return true; } return false;} templateostream& operator<<(ostream& os, cirQueue& queue) { os << "队列中的元素数量为:" << queue.getSize() << endl; return os;} //判断队列是否已满templatebool cirQueue::_full()const { if (front - rear == 1 || front - rear == -maxsize + 1) return true; return false;} #endif // !CIRCULARQUEUE_H

main.cpp

#include"CircularQueue.h"  int main(){ cirQueue cq(20); int a = 0; for (int i = 0; i < 19; i++) { cq.push(i); } cq.print(); cout << cq; for (int i = 0; i < 20; i++) { cq.pop(a); } cout << cq;//此时front=rear=19 cout << cq.getfront() << "  " << cq.getrear() << endl; //for (int i = 19; i < 25; i++) //{ // cq.push(i); //} cq.push(19); cq.print(); cout << cq.getfront() << "  " << cq.getrear() << endl; cout << endl << endl; cq.push(20);  cq.getTop(a); cout << a << endl; cq.print(); cout << cq.getfront() << "  " << cq.getrear() << endl; return 1;}

以上就是C++中怎么实现一个循环队列,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。


网站栏目:C++中怎么实现一个循环队列-创新互联
分享URL:http://bjjierui.cn/article/cohoie.html

其他资讯