【Weiss】【第03章】练习3.25:数组模拟队列
【练习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;
};
【Weiss】【第03章】练习3.25:数组模拟队列的更多相关文章
- uva 12100 Printer Queue 优先级队列模拟题 数组模拟队列
题目很简单,给一个队列以及文件的位置,然后一个一个检查,如果第一个是优先级最高的就打印,否则放到队列后面,求所要打印的文件打印需要花费多长时间. 这里我用数组模拟队列实现,考虑到最糟糕的情况,必须把数 ...
- php中数组模拟队列、栈的函数以及数组指针操作
1,数组指针,current表示当前指针,输出其指向的元素:next表示指针移动到下一个元素:prev指针移动到上一个元素:end表示指针移动到最后一个元素:reset表示指针移动到第一个元素: &l ...
- Java数组模拟队列 + 优化
队列介绍 队列是一个有序列表,可以用数组或是链表来实现. 遵循先入先出的原则. 即:先存入队列的数据,要先取出.后存入的要后取出 示意图:(使用数组模拟队列示意图) 数组模拟队列 队列本身是有序列表 ...
- Java数组模拟队列
队列 先进先出 什么意思呢? 我的理解:队列就是一个数组(不包含链表),然后我们给它施加一个存数据和取数据的规则 当只允许从一端存数据,从另一端取数据的数组,就是队列,我们要做的就是给这个数组施加我们 ...
- 【学习总结】java数据结构和算法-第三章-稀疏数组和队列
相关链接 [学习总结]尚硅谷2019java数据结构和算法 github:javaDSA 目录 稀疏数组 队列 稀疏数组 稀疏数组介绍 图示 应用实例 代码实现 SparseArray.java:与二 ...
- 数据结构算法学习之队列(数组模拟java实现)
数组模拟队列 数组模拟队列 今天学习数组模拟队列.队列常用于生活中的方方面面.比如银行叫号排队.实际上就是队列.所有人抽号排队.先去的先抽号.所以靠前的号最后会先被叫到然后出队.后边的会随之往前移位. ...
- 【Weiss】【第03章】练习3.22、3.23、3.24:无代码题,栈的思考题
[练习3.22] a.提出支持栈的Push和Pop操作以及第三种操作FindMin的数据结构,其中FindMin 返回该数据结构的最小元素,所有操作在最坏情况下的运行时间都是O(1). b.证明,如果 ...
- 《利用python进行数据分析》读书笔记--第四章 numpy基础:数组和矢量计算
http://www.cnblogs.com/batteryhp/p/5000104.html 第四章 Numpy基础:数组和矢量计算 第一部分:numpy的ndarray:一种多维数组对象 实话说, ...
- 《利用Python进行数据分析·第2版》第四章 Numpy基础:数组和矢量计算
<利用Python进行数据分析·第2版>第四章 Numpy基础:数组和矢量计算 numpy高效处理大数组的数据原因: numpy是在一个连续的内存块中存储数据,独立于其他python内置对 ...
随机推荐
- [Redis] Redis哨兵模式部署 - zz胖的博客
1. 部署Redis集群 redis的安装及配置参考[redis部署] 本文以创建一主二从的集群为例. 1.1 部署与配置 先创建sentinel目录,在该目录下创建8000,8001,8002三个以 ...
- [LC] 257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...
- PHP常用接口数据过滤的方法
<?php /** * global.func.php 公共函数库 */ /** * 返回经addslashes处理过的字符串或数组 * @param $string 需要处理的字符串或数组 * ...
- numpy创建的array
import numpy as np array = np.array([[1,2,3], [2,3,4]]) #打印列表 print(array)#是几维的 print('number of dim ...
- Python包管理工具setuptools相关
setup函数常用参数: --name 包名称 --version 包版本 --author ...
- 方兴未艾的云计算:SoCC 2015大会
ACM 云计算研讨会(ACM Symposium on Cloud Computing, 以下简称SoCC)是由SIGMOD(Special Interest Group on Management ...
- Html学习笔记(二) 简单标签
标签的重点 标签的用途 标签在浏览器中的默认样式 <body>标签: 在网页上显示的内容 <p>标签: 添加段落 <hx>标签: 添加标题 标签一共有6个,h1.h ...
- 安卓权威编程指南 挑战练习 25章 深度优化 PhotoGallery 应用
你可能已经注意到了,提交搜索时, RecyclerView 要等好一会才能刷新显示搜索结果.请接受挑战,让搜索过程更流畅一些.用户一提交搜索,就隐藏软键盘,收起 SearchView 视图(回到只显示 ...
- 《N诺机试指南》(八)日期、字符串、排序问题
1.日期问题: 输入: 例题: 代码: #include <stdio.h> #include <bits/stdc++.h> struct node{ int year, m ...
- oa办公系统快速开发工具,助力企业优化升级
随着互联网的快速发展.信息化 IT 技术的不断进步.移动互联新技术的兴起,不管是大的集团企业还是中小型企业,纸质化的办公模式已不能满足现有需求,构建oa平台,为员工提供高效的办公环境尤其重要. 我们先 ...