这里Queue写的跟在STL中的使用方法基本上是一致的,经过了简单的测试。需要注意的是下面测试程序中并没有制定顺序列表(即数组)的大小,类模板中默认为10,当数据量超过指定大小时自动开辟一个更大的数组。
运行测试程序的结果:
源码
//Queue.h
#pragma
template <class T>
class Queue {
public:
void clear();
bool push(const T& item); //inserts element at the end
bool pop(); //removes the first element
T front(); //access the first element
bool empty(); //checks whether the underlying container is empty
bool full(); ////checks whether the underlying container is full
};
template <class T>
class arrQueue : public Queue<T> {
private:
int msize; //存放队列的数组的大小
int mfront; //表示队头所在位置的下标
int mrear; //表示队尾所在位置的下标
T* qu; //存放类型为T的队列元素的数组
public:
arrQueue(int size=10) { //创建队列的实例
msize = size+1;; //浪费一个存储空间,以区别队列空和队列满
qu=new T[msize];
mfront=mrear=0;
}
~arrQueue() { //消除该实例,并释放其空间
delete [] qu;
}
void clear() { //清空队列
mfront = mrear;
}
bool push(const T& item) { //item入队,插入队尾
if (((mrear+1)%msize)==mfront) { //队列已满
int newsize = msize*2;
T* newqu = new T[newsize];
int newfront=0;
int newrear=0;
while(mfront != mrear) { //老队列非空
newqu[newrear]=qu[mfront];
newrear = (newrear+1)%newsize;
mfront = (mfront+1)%msize;
}
delete [] qu;
qu = newqu;
mfront = newfront;
mrear = newrear;
msize = newsize;
}
qu[mrear]=item;
mrear = (mrear+1)%msize; //循环后继
return true;
}
T front() { //返回队头元素
if (mfront == mrear) {
cout << "queue is empty" << endl;
// return false;
}
return qu[mfront];
}
bool pop() { //弹出队头元素
if (mfront == mrear) {
cout << "queue is empty" << endl;
return false;
}
mfront = (mfront+1)%msize;
return true;
}
bool empty() {
if (mfront == mrear)
return true;
else
return false;
}
bool full() {
if (((mrear+1)%msize)==mfront)
return true;
else
return false;
}
};
template <class T>
class Link{
public:
T data; //用于保存结点元素的内容
Link<T> *next; //指向后继结点的指针
Link(const T info, Link<T>* nextValue=NULL) {
data=info;
next= nextValue;
}
Link():next(NULL) {}
};
template <class T>
class lnkQueue:public Queue <T> {
private:
int msize;
Link<T>* mfront;
Link<T>* mrear;
public:
lnkQueue(int size=0) {
msize = size;
mfront = mrear = NULL;
}
~lnkQueue() {
clear();
}
void clear() {
while(mfront != NULL) {
mrear = mfront;
mfront = mfront->next;
delete mrear;
}
mrear = NULL;
msize = 0;
}
bool push(const T& item) {
if (mrear == NULL) {
mfront = mrear = new Link<T>(item);
}
else {
mrear->next = new Link<T>(item);
mrear = mrear->next;
}
msize++;
return true;
}
bool pop() {
if (msize==0) {
cout << "queue is empty" << endl;
return false;
}
Link<T>* tmp;
tmp = mfront;
mfront = mfront->next;
delete tmp;
if (mfront==NULL){
mrear = NULL;
}
msize--;
return true;
}
T front() {
if (msize==0) {
cout << "queue is empty" << endl;
// return false;
}
return mfront->data;
}
bool empty() {
if (msize==0)
return true;
else
return false;
}
};
//source.cpp
#include <iostream>
#include"Queue.h"
using namespace std;
int main() {
arrQueue<int> arrs;
lnkQueue<int> lnks;
for (int i=1; i<=11; i++){
arrs.push(i);
lnks.push(i);
}
int topelem;
cout << "arrQueue中元素依次为:";
while(!arrs.empty()) {
topelem = arrs.front(); //返回栈顶内容但不弹出
cout << topelem << ' ';
arrs.pop(); //弹出栈顶内容且返回其值
}
cout << endl;
cout << "lnkQueue中元素依次为:";
while(!lnks.empty()) {
topelem = lnks.front(); //返回栈顶内容但不弹出
cout << topelem << ' ';
lnks.pop(); //弹出栈顶内容且返回其值
}
cout << endl;
return 0;
}