用C++版数据结构写出一个顺序队列要求:键盘输入10个入队,例如输入0123456789输出9876543210求大神帮忙
1个回答
展开全部
队列的头文件如下:
#ifndef QUEUE_H
#define QUEUE_H
#include <iostream>
using std::cout;
using std::endl;
using std::ostream;
template <class Type>
class Queue {
friend ostream & operator<< (ostream &, const Queue<Type> &);
public:
static const int DefaultSize;
Queue(int = DefaultSize); //创建一个最大容量为MaxQueueSize的空队列
Queue(Type [], int, int = DefaultSize);
~Queue() {delete [] queue;}
bool IsFull(); //若元素个数等于队列的最大容量则返回true,否则返回false
void Add(const Type &); //若IsFull()为true,调用外处理函数QueueFull(),否则将item插入队尾
bool IsEmpty() const; //若队列中元素个数等于0,则返回true,否则返回false
Type * Delete(Type &); //若IsEmpty()为true,调用QueueEmpty()并返回0,否则删除队列前段元素并返回其指针
void Empty(); //清空队列
void QueueFull(); //扩充1倍的存储空间
void QueueEmpty(); //提示队列已空,元素不能出队
private:
int front, rear;
Type * queue;
int maxSize;
};
template <class Type>
const int Queue<Type>::DefaultSize = 10;
template <class Type>
Queue<Type>::Queue(int pMaxSize) {
queue = new Type [pMaxSize];
maxSize = pMaxSize;
front = rear = -1;
}
template <class Type>
Queue<Type>::Queue(Type pArray[], int pSize, int pMaxSize) {
queue = new Type [pMaxSize];
for (int i = 0; i < pSize; i++)
{
queue[i] = pArray[i];
}
front = -1; rear = pSize - 1;
maxSize = pMaxSize;
}
template <class Type>
bool Queue<Type>::IsFull() {
if ( rear == maxSize - 1 ) return true;
else return false;
}
template <class Type>
bool Queue<Type>::IsEmpty() const {
if ( rear == front ) return true;
else return false;
}
template <class Type>
void Queue<Type>::Add(const Type & pX) {
if (IsFull())
{
QueueFull();
queue[++rear] = pX;
}
else
{
queue[++rear] = pX;
}
}
template <class Type>
Type * Queue<Type>::Delete(Type & pX) {
if (IsEmpty())
{
QueueEmpty();
return 0;
}
else
{
pX = queue[++front];
return &pX;
}
}
template <class Type>
void Queue<Type>::QueueEmpty() {
cout << "队列为空,不能进行出队操作!" << endl;
}
template <class Type>
void Queue<Type>::QueueFull() {
Type * newQueue = new Type [maxSize * 2];
for ( int i = front + 1; i <= rear; i++ )
{
newQueue[i] = queue[i];
}
maxSize = maxSize * 2;
delete [] queue;
queue = newQueue;
cout << "队列已满,现已增加空间为原来的2倍 " << maxSize;
}
template <class Type>
void Queue<Type>::Empty() {
front = rear = -1;
}
template <class Type>
ostream & operator<< (ostream & pOutput, const Queue<Type> & pQ) {
if (pQ.IsEmpty())
{
cout << "空队列" << endl;
return pOutput;
}
for ( int i = pQ.front + 1; i <= pQ.rear; i++ )
{
pOutput << pQ.queue[i] << " ";
}
cout << endl;
return pOutput;
}
#endif
#ifndef QUEUE_H
#define QUEUE_H
#include <iostream>
using std::cout;
using std::endl;
using std::ostream;
template <class Type>
class Queue {
friend ostream & operator<< (ostream &, const Queue<Type> &);
public:
static const int DefaultSize;
Queue(int = DefaultSize); //创建一个最大容量为MaxQueueSize的空队列
Queue(Type [], int, int = DefaultSize);
~Queue() {delete [] queue;}
bool IsFull(); //若元素个数等于队列的最大容量则返回true,否则返回false
void Add(const Type &); //若IsFull()为true,调用外处理函数QueueFull(),否则将item插入队尾
bool IsEmpty() const; //若队列中元素个数等于0,则返回true,否则返回false
Type * Delete(Type &); //若IsEmpty()为true,调用QueueEmpty()并返回0,否则删除队列前段元素并返回其指针
void Empty(); //清空队列
void QueueFull(); //扩充1倍的存储空间
void QueueEmpty(); //提示队列已空,元素不能出队
private:
int front, rear;
Type * queue;
int maxSize;
};
template <class Type>
const int Queue<Type>::DefaultSize = 10;
template <class Type>
Queue<Type>::Queue(int pMaxSize) {
queue = new Type [pMaxSize];
maxSize = pMaxSize;
front = rear = -1;
}
template <class Type>
Queue<Type>::Queue(Type pArray[], int pSize, int pMaxSize) {
queue = new Type [pMaxSize];
for (int i = 0; i < pSize; i++)
{
queue[i] = pArray[i];
}
front = -1; rear = pSize - 1;
maxSize = pMaxSize;
}
template <class Type>
bool Queue<Type>::IsFull() {
if ( rear == maxSize - 1 ) return true;
else return false;
}
template <class Type>
bool Queue<Type>::IsEmpty() const {
if ( rear == front ) return true;
else return false;
}
template <class Type>
void Queue<Type>::Add(const Type & pX) {
if (IsFull())
{
QueueFull();
queue[++rear] = pX;
}
else
{
queue[++rear] = pX;
}
}
template <class Type>
Type * Queue<Type>::Delete(Type & pX) {
if (IsEmpty())
{
QueueEmpty();
return 0;
}
else
{
pX = queue[++front];
return &pX;
}
}
template <class Type>
void Queue<Type>::QueueEmpty() {
cout << "队列为空,不能进行出队操作!" << endl;
}
template <class Type>
void Queue<Type>::QueueFull() {
Type * newQueue = new Type [maxSize * 2];
for ( int i = front + 1; i <= rear; i++ )
{
newQueue[i] = queue[i];
}
maxSize = maxSize * 2;
delete [] queue;
queue = newQueue;
cout << "队列已满,现已增加空间为原来的2倍 " << maxSize;
}
template <class Type>
void Queue<Type>::Empty() {
front = rear = -1;
}
template <class Type>
ostream & operator<< (ostream & pOutput, const Queue<Type> & pQ) {
if (pQ.IsEmpty())
{
cout << "空队列" << endl;
return pOutput;
}
for ( int i = pQ.front + 1; i <= pQ.rear; i++ )
{
pOutput << pQ.queue[i] << " ";
}
cout << endl;
return pOutput;
}
#endif
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询