文件:Queue.h
#ifndef QUEUE_H_
#define QUEUE_H_
template <class T>
class Queue
{
public:
Queue(){} //构造函数
virtual ~Queue(){} //析构函数
public:
virtual bool EnQueue(const T& x) = 0; //新元素x入队
virtual bool DeQueue(T& x) = 0; //队头元素出队,并将该元素的值保存至x
virtual bool getFront(T& x) const = 0; //读取队头元素,并将该元素的值保存至x
virtual bool IsEmpty() const = 0; //判断队列是否为空
virtual bool IsFull() const = 0; //判断队列是否为满
virtual int getSize() const = 0; //计算队列中元素个数
virtual void MakeEmpty() = 0; //清空队列的内容
};
#endif /* QUEUE_H_ */
文件:CircularQueue.h
#ifndef CIRCULAR_QUEUE_H_
#define CIRCULAR_QUEUE_H_
#include "Queue.h"
#include <iostream>
#include <string>
#include <strstream>
using namespace std;
const int defaultSize = 50;
template <class T>
class CircularQueue : public Queue<T>
{
public:
CircularQueue(int sz = defaultSize); //构造函数
virtual ~CircularQueue(); //析构函数
public:
virtual bool EnQueue(const T& x); //新元素x入队
virtual bool DeQueue(T& x); //队头元素出队,并将该元素的值保存至x
virtual bool getFront(T& x) const; //读取队头元素,并将该元素的值保存至x
virtual bool IsEmpty() const; //判断队列是否为空
virtual bool IsFull() const; //判断队列是否为满
virtual int getSize() const; //计算队列中元素个数
virtual void MakeEmpty(); //清空队列的内容
public:
template <class T>
friend ostream& operator<<(ostream& os, const CircularQueue<T>& s); //输出队列元素的重载操作<<
private:
T *elements; //存放队列元素的队列数组
int front; //队头指针
int rear; //队尾指针
int maxSize; //队列最大可容纳元素个数
};
//构造函数
template <class T>
CircularQueue<T>::CircularQueue(int sz)
{
cout << "$ 执行构造函数" << endl;
if (sz >= 0)
{
maxSize = sz;
front = rear = 0;
elements = new T[maxSize];
}
}
//析构函数
template <class T>
CircularQueue<T>::~CircularQueue()
{
cout << "$ 执行析构函数" << endl;
delete[] elements;
elements = NULL;
}
//新元素x入队
template <class T>
bool CircularQueue<T>::EnQueue(const T& x)
{
if (true == IsFull())
{
return false;
}
elements[rear] = x;
rear = (rear + 1) % maxSize;
return true;
}
//队头元素出队,并将该元素的值保存至x
template <class T>
bool CircularQueue<T>::DeQueue(T& x)
{
if (true == IsEmpty())
{
return false;
}
x = elements[front];
front = (front + 1) % maxSize;
return true;
}
//读取队头元素,并将该元素的值保存至x
template <class T>
bool CircularQueue<T>::getFront(T& x) const
{
if (true == IsEmpty())
{
return false;
}
x = elements[front];
return true;
}
//判断队列是否为空
template <class T>
bool CircularQueue<T>::IsEmpty() const
{
return (front == rear) ? true : false;
}
//判断队列是否为满
template <class T>
bool CircularQueue<T>::IsFull() const
{
return ((rear + 1) % maxSize == front) ? true : false;
}
//计算队列中元素个数
template <class T>
int CircularQueue<T>::getSize() const
{
return (rear - front + maxSize) % maxSize;
}
//清空队列的内容
template <class T>
void CircularQueue<T>::MakeEmpty()
{
delete[] elements;
front = rear = 0;
elements = new T[maxSize];
}
//输出队列元素的重载操作<<
template <class T>
ostream& operator<<(ostream& os, const CircularQueue<T>& q)
{
for (int i = q.front; i != q.rear; i = (i+1)%q.maxSize)
{
os << "[" << i << "]" << " : " << q.elements[i] << endl;
}
return os;
}
#endif /* CIRCULAR_QUEUE_H_ */
文件:main.cpp
#include "CircularQueue.h"
#define EXIT 0 //退出
#define ENQUEUE 1 //新元素x入队
#define DEQUEUE 2 //队头元素出队,并将该元素的值保存至x
#define GETFRONT 3 //读取队头元素,并将该元素的值保存至x
#define ISEMPTY 4 //判断队列是否为空
#define ISFULL 5 //判断队列是否为满
#define GETSIZE 6 //计算队列中元素个数
#define MAKEEMPTY 7 //清空队列的内容
#define OPERATOR_OSTREAM 8 //输出队列元素的重载操作<<
void print_description()
{
cout << "------------------------------>循环队列<------------------------------" << endl;
cout << "功能选项说明:" << endl;
cout << "#0: 退出" << endl;
cout << "#1: 新元素x入队" << endl;
cout << "#2: 队头元素出队,并将该元素的值保存至x" << endl;
cout << "#3: 读取队头元素,并将该元素的值保存至x" << endl;
cout << "#4: 判断队列是否为空" << endl;
cout << "#5: 判断队列是否为满" << endl;
cout << "#6: 计算队列中元素个数" << endl;
cout << "#7: 清空队列的内容" << endl;
cout << "#8: 输出队列元素的重载操作<<" << endl;
cout << "--------------------------------------------------------------------" << endl;
}
//判断输入的字符串每个字符是否都是数值0~9
bool IsNumber(const string& s_num)
{
for (size_t i = 0; i < s_num.size(); i++)
{
if ((s_num[i] < '0') || (s_num[i] > '9'))
{
return false;
}
}
return true;
}
//类型转换——将string型转为模板类型T
template <class T>
T StrToTtype(const string& s_num)
{
T n_num;
strstream ss_num;
ss_num << s_num;
ss_num >> n_num;
return n_num;
}
//输入数据值
template <class T>
T get_data()
{
cout << "> 请输入数据值,data = ";
string s_data;
cin >> s_data;
return StrToTtype<T>(s_data);
}
//输入数组的最大长度
template <class T>
int get_maxsize()
{
cout << "> 请输入数组的最大长度,maxsize = ";
string s_maxsize;
cin >> s_maxsize;
while (false == IsNumber(s_maxsize))
{
cout << "* 输入有误,请重新输入:";
cin >> s_maxsize;
}
return atoi(s_maxsize.c_str());
}
//构造循环队列
template <class T>
CircularQueue<T>* construct_cirqueue()
{
cout << "\n==> 创建循环队列" << endl;
int n_maxsize = get_maxsize<T>();
CircularQueue<T> *cirQueue = new CircularQueue<T>(n_maxsize);
return cirQueue;
}
//析构循环队列
template <class T>
void destory_cirqueue(CircularQueue<T>* cirQueue)
{
cout << "\n==> 释放循环队列在堆中申请的空间,并将指向该空间的指针变量置为空" << endl;
delete cirQueue;
cirQueue = NULL;
}
//新元素x入队
template <class T>
void enqueue(CircularQueue<T>* cirQueue)
{
cout << "$ 执行新元素x入队函数" << endl;
T data = get_data<T>();
if (false == cirQueue->EnQueue(data))
{
cout << "* 入队失败" << endl;
return;
}
cout << "* 入队成功,data = " << data << endl;
}
//队头元素出队,并将该元素的值保存至x
template <class T>
void dequeue(CircularQueue<T>* cirQueue)
{
cout << "$ 执行队头元素出队并将该元素的值保存至x函数" << endl;
T data;
if (false == cirQueue->DeQueue(data))
{
cout << "* 出队失败" << endl;
return;
}
cout << "* 出队成功,data = " << data << endl;
}
//读取队头元素,并将该元素的值保存至x
template <class T>
void getfront(CircularQueue<T>* cirQueue)
{
cout << "$ 执行读取队头元素并将该元素的值保存至x函数" << endl;
T data;
if (false == cirQueue->getFront(data))
{
cout << "* 读取队头元素失败" << endl;
return;
}
cout << "* 读取队头元素成功,data = " << data << endl;
}
//判断队列是否为空
template <class T>
void isempty(CircularQueue<T>* cirQueue)
{
cout << "$ 执行判断队列是否为空函数,IsEmpty = " << cirQueue->IsEmpty() << endl;
}
//判断队列是否为满
template <class T>
void isfull(CircularQueue<T>* cirQueue)
{
cout << "$ 执行判断队列是否为满函数,IsFull = " << cirQueue->IsFull() << endl;
}
//计算队列中元素个数
template <class T>
void getsize(CircularQueue<T>* cirQueue)
{
cout << "$ 执行计算队列中元素个数函数,Size = " << cirQueue->getSize() << endl;
}
//清空队列的内容
template <class T>
void makeempty(CircularQueue<T>* cirQueue)
{
cout << "$ 执行清空队列的内容函数" << endl;
cirQueue->MakeEmpty();
}
//输出队列元素的重载操作<<
template <class T>
void operator_ostream(CircularQueue<T>* cirQueue)
{
cout << "$ 执行输出队列元素的重载操作<<函数" << endl;
cout << *cirQueue;//或operator<<(cout, *cirQueue);
}
//循环队列操作选择
template <class T>
void select_operation(CircularQueue<T>* cirQueue)
{
if (NULL == cirQueue)
{
cout << "* 没有构造循环队列,请先构造循环队列。" << endl;
return;
}
string s_operation;
while (s_operation != "0")
{
cout << "\n==> 请输入功能选项编号(按\"0\"退出程序):";
cin >> s_operation;
while (false == IsNumber(s_operation))
{
cout << "* 输入有误,请重新输入:";
cin >> s_operation;
}
int n_operation = atoi(s_operation.c_str());
switch (n_operation)
{
case EXIT://退出
{
cout << "$ 退出程序" << endl;
break;
}
case ENQUEUE://新元素x入队
{
enqueue(cirQueue);
break;
}
case DEQUEUE://队头元素出队,并将该元素的值保存至x
{
dequeue(cirQueue);
break;
}
case GETFRONT://读取队头元素,并将该元素的值保存至x
{
getfront(cirQueue);
break;
}
case ISEMPTY://判断队列是否为空
{
isempty(cirQueue);
break;
}
case ISFULL://判断队列是否为满
{
isfull(cirQueue);
break;
}
case GETSIZE://计算队列元素个数
{
getsize(cirQueue);
break;
}
case MAKEEMPTY://清空队列的内容
{
makeempty(cirQueue);
break;
}
case OPERATOR_OSTREAM://输出队列元素的重载操作<<
{
operator_ostream(cirQueue);
break;
}
default:
{
cout << "* 请输入正确的功能选项编号" << endl;
break;
}
}
}
}
int main(int argc, char* argv[])
{
print_description();
CircularQueue<int> *cirQueue = construct_cirqueue<int>();
select_operation(cirQueue);
destory_cirqueue(cirQueue);
system("pause");
return 0;
}
参考文献:
[1]《数据结构(用面向对象方法与C++语言描述)(第2版)》殷人昆——第三章
[2]《C/C++常用算法手册》秦姣华、向旭宇——第二章
[3] 百度搜索关键字:循环队列