【练习3.25】

编写实现队列的例程,使用

a.链表

b.数组

Answer:

在这章一开头就已经写了个链表的队列例程了,所以实际上只要做b小题就可以。

数组模拟队列和链表的两点小不同是:

①、数组空间有限,入队需要检测数组是否已经满

②、数组经过几次操作后,rear可能绕回front前面,所以许多操作都要用模来实现。

测试代码:

 #include <iostream>
#include "queue.h"
using namespace std;
using namespace queue;
template class Queue<int>;
int main(void)
{
Simu_Queue<int> test();
//测试插入
test.enqueue();
test.enqueue();
test.enqueue();
test.enqueue();
test.enqueue();
test.enqueue();
test.enqueue();
test.traverse();
cout << endl;
//测试删除
test.dequeue();
test.dequeue();
test.dequeue();
test.traverse();
cout << endl;
//测试绕数组遍历
test.enqueue();
test.enqueue();
test.enqueue();
test.traverse();
cout << endl; system("pause");
}

实现代码:

 //练习3.25新增,用数组模拟队列
template <typename T> class Simu_Queue
{
public:
Simu_Queue() :head(nullptr), front(), rear(), size(){}
Simu_Queue(unsigned int _maxsize) :front(), rear(), maxsize(_maxsize){ head = new T[maxsize + ]; }
Simu_Queue(const Simu_Queue& another)
{
front = another.front;
rear = another.rear;
maxsize = another.maxsize;
head = new T[maxsize + ];
for (unsigned i = ; i < maxsize + ; ++i)
head[i] = another.head[i];
}
~Simu_Queue()
{
delete[] head;
front = rear = maxsize = ;
head = nullptr;
}
Simu_Queue& operator=(const Simu_Queue& another)
{
if (this != &another)
{
delete[] head;
front = another.front;
rear = another.rear;
maxsize = another.maxsize;
head = new T[maxsize + ];
for (unsigned i = ; i < maxsize + ; ++i)
head[i] = another.head[i];
}
}
public:
//返回最大元素量
unsigned int size()const{ return maxsize; }
//返回当前元素量
unsigned int length()const{ return front <= rear ? rear - front : rear + maxsize + - front; }
//判断是否为空
bool empty()const{ return front == rear; }
//入队
bool enqueue(const T &item)
{
if ((rear + ) % (maxsize + ) != front)
{
head[rear] = item;
rear = (rear + ) % (maxsize + );
return true;
}
return false;
}
//出队
bool dequeue()
{
if (rear != front)
{
front = (front + ) % (maxsize + );
return true;
}
return false;
}
//输出队列元素
void traverse()const
{
unsigned int temp = front;
while (temp != rear)
{
cout << " " << head[temp] << flush;
temp = (temp + ) % (maxsize + );
}
}
private:
T* head = nullptr;
unsigned int front;
unsigned int rear;
unsigned int maxsize;
};

最新文章

  1. 文件过滤驱动框架Minispy解析一
  2. C++笔记 之 基础回顾(一)
  3. oracle11g AUD$维护
  4. Android自动化测试中Monkeyrunner详解
  5. invalid END header (bad central directory offset) 异常解决方法
  6. Notepad++ HTML格式化
  7. Bash简介
  8. 正确的安装和使用nvm
  9. debug(fmt,args...)调试
  10. [FindBugs分析记录]Potentially dangerous use of non-short-circuit logic
  11. WPF制作的一个小功能,智能提示(IntelliSense)
  12. char *和char[]的区别,困扰很长时间了,总结下
  13. Getting Started with Core Data
  14. 基于gulp编写的一个简单实用的前端开发环境好了,安装完Gulp后,接下来是你大展身手的时候了,在你自己的电脑上面随便哪个地方建一个目录,打开命令行,然后进入创建好的目录里面,开始撸代码,关于生成的json文件请点击这里https://docs.npmjs.com/files/package.json,打开的速度看你的网速了注意:以下是为了演示 ,我建的一个目录结构,你自己可以根据项目需求自己建目
  15. 部署开启了Kerberos身份验证的大数据平台集群外客户端
  16. HDU 2087 剪花布条(KMP基础应用)
  17. 【野草】SQL Server之索引解析(一)
  18. C# 数组、HashSet等内存耗尽的解决办法
  19. oc之考试答题类效果
  20. at android.view.Surface.unlockCanvasAndPost(Native Method)

热门文章

  1. C++中的大数乘的实现
  2. 利用Load命令将本地文本里面的数据导入到MySQL数据库
  3. &lt;BZOJ3032&gt;七夕祭
  4. xshell 常用命令1
  5. 根据现有的数据库自动生成Django model
  6. iOS自建分发
  7. DJI大疆创新招聘-自动化测试工程师
  8. android编译架构之添加C项目
  9. 启动tomcat报错 Unable to process Jar entry [module-info.class] from Jar...[xxx.xx.jar!\] for annotations
  10. tp5.1 请求时间格式化