队列(queue)是一种限定存取位置的线性变。他允许在表的一端插入,在另一端删除。这个和计算机调度策略中的先来先服务FCFS(First Come/First Served)是一样的。队列中可以插入的一端为队尾(rear),允许删除的一端称为队头(front)。

队列也分为两种,一种是用数组的存储表示,一种是基于链表的存储表示。

基于数组的存储表示的队列被称为顺序队列。其数据成员包括,一维数组elements用来存储数据,指针front和rear用来指示队尾队头的位置,maxSize是数组的最大长度。

从上边的图可以看出来,rear指针到指到数组最后一位时就不能继续往后添加元素,如果之前删除过元素,front指针前还有空闲的空间未被使用,造成空间的浪费。所以,使队列循环起来就可以使其最大限度的利用空间。

变成循环队列,为了避免越界,每次添加新元素时,尾指针需要加一后对堆长取余

rear = (rear+1)%maxSize;

删除元素也一样

front = (front+1)%maxSize;

为了区别于空队列,用(rear+1)%maxSize==front来判断是否队满,即队尾走到队头前一个位置即判定队满。再因为队尾所指的空间为最后一个元素的下一个位置,所以循环队列最多能存放maxSize-1个元素。

//queue.h
#ifndef _QUEUE_H
#define _QUEUE_H
#include<iostream>
using namespace std;
const int maxSize = 50;
template<class T>
class Queue
{
public:
Queue(){};
~Queue(){};
virtual bool EnQueue(const T& x) = 0;
virtual bool DeQueue(T& x) = 0;
virtual bool getFront(T& x) = 0;
virtual bool IsEmpty()const = 0;
virtual bool IsFull()const = 0;
virtual int getSize()const = 0;
};
#endif
//main.cpp
#include<assert.h>
#include"queue.h"
template<class T>
class SeqQueue;
template<class T>
ostream& operator<< (ostream& out, SeqQueue<T>& Q)//将友元函数声明在前可以避免其警告友元函数未声明
{
cout<<"front = "<<Q.front<<", rear = "<<Q.rear<<endl;
if(!Q.IsEmpty())
{
int i = Q.front;
while(i!=Q.rear)
{
cout<<Q.elements[i]<<" | ";
i = (++i)%maxSize;
}
}
return out; } template<class T>
class SeqQueue: public Queue<T>
{
private:
int rear, front;
T *elements;
int maxSize;
public:
SeqQueue(int sz=10);//构造函数
~SeqQueue(){delete[] elements;}//析构函数
bool EnQueue(const T& x);//入队列
bool DeQueue(T& x);//出队列
bool getFront(T& x);//找队头
bool IsEmpty()const{return (this->rear==this->front) ? true : false;}//判空
bool IsFull()const{return ((this->rear+1)%this->maxSize==this->front) ? true : false;}//判满
int getSize()const{return(this->rear-this->front+this->maxSize)%this->maxSize;}//得队长
friend ostream& operator<<<>(ostream& out, SeqQueue<T>& Q);
//void print();
};
/*template<class T>
void SeqQueue<T>::print()
{
if(!this->IsEmpty())
{
int i = this->front;
while(i!=this->rear)
{
cout<<this->elements[i]<<" | ";
i = (++i)/this->maxSize;
}
}
}*/
template<class T>
bool SeqQueue<T>::EnQueue(const T& x)
{
if(!this->IsFull())
{
this->elements[this->rear] = x;
this->rear = (this->rear+1)%this->maxSize;
return true;
}
return false;
}
template<class T>
bool SeqQueue<T>::DeQueue(T& x)
{
if(!this->IsEmpty())
{
x = this->elements[this->front];
this->front = (this->front+1)%this->maxSize;
return true;
}
return false;
}
template<class T>
bool SeqQueue<T>::getFront(T& x)
{
if(!this->IsEmpty())
{
x = this->elements[this->front];
return true;
}
return false;
}
template<class T>
SeqQueue<T>::SeqQueue(int sz):front(0),rear(0),maxSize(sz)
{
this->elements = new T[maxSize];
assert(this->elements!=NULL);
}
int main()
{
SeqQueue<int> Q(7);
for(int i=7; i>0; --i)
Q.EnQueue(i);
cout<<Q<<endl;//声明友元函数并重载输出运算符<<就可以用cout直接输出对象
int q = 0;
for(int i=3; i>=0; --i)
Q.DeQueue(q);
cout<<Q<<endl;
cout<<"Size = "<<Q.getSize()<<endl;
return 0;
}

运行结果

最新文章

  1. 初学C++ 之 auto关键字(IDE:VS2013)
  2. hihoCoder 1185 连通性&#183;三(Tarjan缩点+暴力DFS)
  3. R语言学习
  4. 安卓自动化测试(2)Robotium环境搭建与新手入门教程
  5. 以一个权限系统来告别WebForm —(一)项目整休架构设计与数据库设计
  6. C# 对Xml的常用操作
  7. WordPress 撰写文章页面显示所有标签
  8. 使用Git和远程代码库
  9. 写一个TT模板自动生成spring.net下面的配置文件。
  10. 在SSH框架中增加SiteMesh的支持
  11. 转;说说AngularJS中的$parse和$eval
  12. box-shadow 被其他div遮住 shadow was hidden/covered by another div
  13. 07_数据库创建,添加c3p0操作所需的jar包,编写c3p0-config.xml文件,编写User.java,编写jdbcUtils.java实现操作数据库的模板工具类,UserDao编写,Dao
  14. python3 json模块
  15. leetcode 395. Longest Substring with At Least K Repeating Characters(高质量题)
  16. 【MVC】快速构建一个图片浏览网站
  17. MATLAB总结二
  18. Disabling the Windows 8 UAC
  19. 王者荣耀交流协会互评Beta版本及答复功能改进建议、Bug修正
  20. 多线程在javaweb中的应用

热门文章

  1. hdu 5087 Revenge of LIS II (DP)
  2. 面试官:JavaScript如何实现数组拍平(扁平化)方法?
  3. x64 InlineHook 黑魔法
  4. 精心整理Java微服务最全面试题集(含答案)
  5. echarts 让轴自适应数据为小数整数
  6. Python 数据类型常用的内置方法(一)
  7. 查看python是32位,还是64位
  8. 使用.NET5、Blazor和Electron.NET构建跨平台桌面应用
  9. SQLServer创建约束
  10. 关于 RocketMQ ClientID 相同引发的消息堆积的问题