当前位置:网站首页>数组实现可扩展的循环队列
数组实现可扩展的循环队列
2022-07-21 15:11:00 【小飞将】
arrayQueue.h
// circular array implementation of a queue
// derives from the ADT queue
#ifndef arrayQueue_
#define arrayQueue_
#include "queue.h"
#include "myExceptions.h"
#include <sstream>
using namespace std;
template<class T>
class arrayQueue : public queue<T>
{
public:
arrayQueue(int initialCapacity = 10);
~arrayQueue() {
delete [] queue;}
bool empty() const {
return theFront == theBack;}
int size() const
{
return (theBack - theFront + arrayLength) % arrayLength;}
T& front()
{
// return front element
if (theFront == theBack)
throw queueEmpty();
return queue[(theFront + 1) % arrayLength];
}
T& back()
{
// return theBack element
if (theFront == theBack)
throw queueEmpty();
return queue[theBack];
}
void pop()
{
// remove theFront element
if (theFront == theBack)
throw queueEmpty();
theFront = (theFront + 1) % arrayLength;
queue[theFront].~T(); // destructor for T
}
void push(const T& theElement);
private:
int theFront; // 1 counterclockwise from theFront element
int theBack; // position of theBack element
int arrayLength; // queue capacity
T *queue; // element array
};
template<class T>
arrayQueue<T>::arrayQueue(int initialCapacity)
{
// Constructor.
if (initialCapacity < 1)
{
ostringstream s;
s << "Initial capacity = " << initialCapacity << " Must be > 0";
throw illegalParameterValue(s.str());
}
arrayLength = initialCapacity;
queue = new T[arrayLength];
theFront = 0;
theBack = 0;
}
template<class T>
void arrayQueue<T>::push(const T& theElement)
{
// Add theElement to queue.
// increase array length if necessary
if ((theBack + 1) % arrayLength == theFront)
{
// double array length
// allocate a new array
T* newQueue = new T[2 * arrayLength];
// copy elements into new array
int start = (theFront + 1) % arrayLength;
if (start < 2)
// no wrap around
copy(queue + start, queue + start + arrayLength - 1, newQueue);
else
{
// queue wraps around
copy(queue + start, queue + arrayLength, newQueue);
copy(queue, queue + theBack + 1, newQueue + arrayLength - start);
}
// switch to newQueue and set theFront and theBack
theFront = 2 * arrayLength - 1;
theBack = arrayLength - 2; // queue size arrayLength - 1
arrayLength *= 2;
queue = newQueue;
}
// put theElement at the theBack of the queue
theBack = (theBack + 1) % arrayLength;
queue[theBack] = theElement;
}
#endif
main.cpp
// test array queue
#include <iostream>
#include "arrayQueue.h"
#include "myExceptions.h"
using namespace std;
int main(void)
{
arrayQueue<int> q(4);
// add a few elements
q.push(1);
cout << "Queue rear is " << q.back() << endl;
q.push(2);
cout << "Queue rear is " << q.back() << endl;
q.push(3);
cout << "Queue rear is " << q.back() << endl;
q.push(4);
cout << "Queue rear is " << q.back() << endl;
cout << "Queue should be 1234, front to rear" << endl;
// test empty and size
if (q.empty())
cout << "The queue is empty" << endl;
else
cout << "The queue is not empty" << endl;
cout << "The queue size is " << q.size() << endl;
while (!q.empty())
{
cout << "Queue front is " << q.front() << endl;
q.pop();
cout << "Popped front element" << endl;
}
try {
q.pop();}
catch (queueEmpty message)
{
cout << "Last pop failed " << endl;
message.outputMessage();
}
// create a wraparound queue and do array doubling
arrayQueue<int> r(4);
r.push(1);
r.push(2);
r.push(3);
r.pop();
r.pop();
r.push(4);
r.push(5);
r.push(6);
r.push(7);
cout << "Queue should be 34567, front to rear" << endl;
// test empty and size
if (r.empty())
cout << "The queue is empty" << endl;
else
cout << "The queue is not empty" << endl;
cout << "The queue size is " << r.size() << endl;
while (!r.empty())
{
cout << "Queue front is " << r.front() << endl;
r.pop();
cout << "Popped front element" << endl;
}
return 0;
}
问题:
- 当copy原队列至新队列时,
start
与2的关系,是什么意义?
int start = (theFront + 1) % arrayLength;
if (start < 2)
// no wrap around
copy(queue + start, queue + start + arrayLength - 1, newQueue);
else
{
// queue wraps around
copy(queue + start, queue + arrayLength, newQueue);
copy(queue, queue + theBack + 1, newQueue + arrayLength - start);
}
- 当新队列拷贝完成后,没有看到老队列的delete操作。
边栏推荐
猜你喜欢
敬伟PS教程:基础篇A
Matlab GUI编程技巧(七):matlablistbox操作-列表框(ListBox)和uilistbox常用操作
Matlab GUI编程技巧(九):详解 uitable 函数显示表格及相关操作(创建表用户界面)
QT - graphic view
Qt - vue graphique
笔记本电脑选购指南(不推荐具体品牌及型号)
EMQ映云科技荣登《中国企业家》2022年度“新锐100”榜单
Jmeter安装与设置(一)
B tree and b+ tree hash index
Lodash中数组和对象合并方法assign、merge、defaults、defaultsDeep比较
随机推荐
查找表中重复内容
After uniapp sets tabbar jump, other pages jump to the home page (sorting)
1024节日快乐
链表结构的栈实现
Basic exercises of C language
零碎知识——sql相关
VLOOKUP函数
C语言基础练习题
ArcGIS创建矢量
Observer mode and publish / subscribe mode
用原生js实现放大镜功能
Vim Ctags
面试算法题
uniapp设置tabBar跳转后,其他页面跳转到主页(整理)
微信小程序之侧边列表菜单(侧边菜单)的实现
运行时,物体移动旋转缩放插件,“RuntimeTransformGizmos插件”使用教程(Unity3D)
MOD17A3HGF数据下载
flask - { “message“: “Failed to decode JSON object: Expecting value: line 1 column 1 (char 0)“ }
Love running every day [noip2016 T4]
Foundation of Mathematics: Jensen inequality