C++实现循环队列

 更新时间:2020年4月25日 17:24  点击:1742

本文实例为大家分享了C++实现循环队列的具体代码,供大家参考,具体内容如下

circularQueue.h

#pragma once
#pragma once
#ifndef CIRCULARQUEUE_H
#define CIRCULARQUEUE_H
 
#include<iostream>
#include<ostream>
using std::cout;
using std::cin;
using std::endl;
using std::ostream;
template<class T> class cirQueue;
 
template<typename T>
class 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<typename T>
 friend ostream& operator<<(ostream& os, cirQueue<T>& queue);
 
private:
 bool _full()const;//判断队列是否已满
 int maxsize;//队列最大的空间
 T* element;//存放于队列中的元素数组
 int front;//模拟队头指针
 int rear;//模拟队尾指针
};
 
 
template<typename T>
cirQueue<T>::cirQueue(int sz) {
 maxsize = sz;
 element = new T[maxsize];
 if (element == nullptr)
 cout << "内存分配失败" << endl;
 front = 0;
 rear = 0;
}
 
 
template<typename T>
cirQueue<T>::~cirQueue() {
 if (element != nullptr)
 delete element;
}
 
//进队
template<typename T>
void cirQueue<T>::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;
 }
}
 
//出队
template<typename T>
void cirQueue<T>::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;
 }
}
 
//查看队列是否为空
template<typename T>
bool cirQueue<T>::empty() {
 if (front == rear)//待定
 return true;
 return false;
}
 
//返回队列中元素的个数
template<typename T>
int cirQueue<T>::getSize() {
 int num = 0;
 if (front <= rear)
 return rear - front;
 else
 return maxsize - front + rear + 1;
}
 
//清空队列中的元素
template<typename T>
void cirQueue<T>::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;
  }
 }
 }
}
 
//打印队列中的元素
template<typename T>
void cirQueue<T>::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;
  }
  }
 }
 }
}
 
//读取队列首个元素
template<typename T>
bool cirQueue<T>::getTop(T& elem) {
 if (!empty()) {
 elem = element[front];
 return true;
 }
 return false;
}
 
template<typename T>
ostream& operator<<(ostream& os, cirQueue<T>& queue) {
 os << "队列中的元素数量为:" << queue.getSize() << endl;
 return os;
}
 
//判断队列是否已满
template<typename T>
bool cirQueue<T>::_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<int> 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;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持猪先飞。

[!--infotagslink--]

相关文章

  • C++ STL标准库std::vector的使用详解

    vector是表示可以改变大小的数组的序列容器,本文主要介绍了C++STL标准库std::vector的使用详解,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2022-03-06
  • C++中取余运算的实现

    这篇文章主要介绍了C++中取余运算的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-23
  • 详解C++ string常用截取字符串方法

    这篇文章主要介绍了C++ string常用截取字符串方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
  • C++调用C#的DLL程序实现方法

    本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
  • C++中四种加密算法之AES源代码

    本篇文章主要介绍了C++中四种加密算法之AES源代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。...2020-04-25
  • C++ 整数拆分方法详解

    整数拆分,指把一个整数分解成若干个整数的和。本文重点给大家介绍C++ 整数拆分方法详解,非常不错,感兴趣的朋友一起学习吧...2020-04-25
  • C++中 Sort函数详细解析

    这篇文章主要介绍了C++中Sort函数详细解析,sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变...2022-08-18
  • C++万能库头文件在vs中的安装步骤(图文)

    这篇文章主要介绍了C++万能库头文件在vs中的安装步骤(图文),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-23
  • 详解C++ bitset用法

    这篇文章主要介绍了C++ bitset用法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
  • 浅谈C++中的string 类型占几个字节

    本篇文章小编并不是为大家讲解string类型的用法,而是讲解我个人比较好奇的问题,就是string 类型占几个字节...2020-04-25
  • C++ Eigen库计算矩阵特征值及特征向量

    这篇文章主要为大家详细介绍了C++ Eigen库计算矩阵特征值及特征向量,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-04-25
  • C++ pair的用法实例详解

    这篇文章主要介绍了C++ pair的用法实例详解的相关资料,需要的朋友可以参考下...2020-04-25
  • VSCode C++多文件编译的简单使用方法

    这篇文章主要介绍了VSCode C++多文件编译的简单使用方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-03-29
  • C++中的循环引用

    虽然C++11引入了智能指针的,但是开发人员在与内存的斗争问题上并没有解放,如果我门实用不当仍然有内存泄漏问题,其中智能指针的循环引用缺陷是最大的问题。下面通过实例代码给大家介绍c++中的循环引用,一起看看吧...2020-04-25
  • C++随机点名生成器实例代码(老师们的福音!)

    这篇文章主要给大家介绍了关于C++随机点名生成器的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
  • C++如何删除map容器中指定值的元素详解

    map容器是C++ STL中的重要一员,删除map容器中value为指定元素的问题是我们经常与遇到的一个问题,下面这篇文章主要给大家介绍了关于利用C++如何删除map容器中指定值的元素的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。...2020-04-25
  • C++ 约瑟夫环问题案例详解

    这篇文章主要介绍了C++ 约瑟夫环问题案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下...2021-08-15
  • C++中cin的用法详细

    这篇文章主要介绍了C++中cin的用法详细,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
  • 基于C++中常见编译错误的总结详解

    本篇文章是对C++中的常见编译错误进行了详细的分析介绍,需要的朋友参考下...2020-04-25
  • c++优先队列(priority_queue)用法详解

    这篇文章主要介绍了c++优先队列(priority_queue)用法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25