动态数组实现循环队列

 VS2005和Dev均可以运行通过,如有问题,请各位大牛指正。

/*动态数组实现循环队列:少用一个空间,队尾指针的下一位是队头指针时为满
队头、队尾初始值:front = rear = 0;
队头:总是指向第一个结点
队尾:总是指向最后一个结点的下一位
队空:front == rear
队满:(rear+1)%Max==front
入队: data[(rear++)%Max]= NewItem;
出队:NewItem = data[(front++)%Max];
*/

#include <iostream>
using namespace std;
const int QueueIncreMent=10; 
template<class Type>
class Queue
{
private:
	Type* data;
	int front;
	int rear;
	int maxSize;//队列的最大空间
public:
	Queue(int maxSize = 20);
	~Queue();
	Queue(const Queue<Type>& otherQueue);
	Queue<Type> operator=(const Queue<Type>& otherQueue);
	void initQueue();
	bool isEmptyQueue() const;
	bool IsFullQueue() const;;
	void destoryQueue();
	void enQueue(Type newItem);
	void deQueue(Type& newItem);
	int lengthQueue();
};

template<class Type>
Queue<Type>::Queue(int maxSize)//不能写成maxSize = 20,否则dev通不过
{
	data = new Type[maxSize];
	front = 0;
	rear = 0;
	this->maxSize = maxSize;
}

template<class Type>
Queue<Type>::~Queue()
{
	if (data!=NULL)
	{
		destoryQueue();
	}
}

template<class Type>
Queue<Type>::Queue(const Queue<Type>& otherQueue)
{
	if (!otherQueue.isEmptyQueue())
	{
		front = otherQueue.front;
		rear = otherQueue.rear;
		maxSize = otherQueue.maxSize;
		data = new Type[maxSize];
		memcpy(data,otherQueue.data,maxSize);
	}
}

template<class Type>
Queue<Type> Queue<Type>::operator=(const Queue<Type>& otherQueue)
{
	if (this != &otherQueue)
	{
		if (!otherQueue.isEmptyQueue())
		{
			if (maxSize!=otherQueue.maxSize)
			{
				maxSize = otherQueue.maxSize;
				delete[] data;
				data = new Type[maxSize];
				
			}
			front = otherQueue.front;
			rear = otherQueue.rear;
			memcpy(data,otherQueue.data,maxSize);
		}
	}
	return *this;
}

template<class Type>
void Queue<Type>::initQueue()
{
	destoryQueue();
}

template<class Type>
bool Queue<Type>::isEmptyQueue() const
{
	return (rear == front);
}

template<class Type>
bool Queue<Type>::IsFullQueue() const
{
	return ((rear+1)%maxSize==front);
}

template<class Type>
void Queue<Type>::destoryQueue()
{
	front = 0;
	rear =0;
	delete[] data;
	data = NULL;
}

template<class Type>
void Queue<Type>::deQueue(Type& NewItem)
{
	if (isEmptyQueue())
	{
		cout<<"栈空"<<endl;
	}
	else
	{
		NewItem = data[(front++)%maxSize];
		/*等价于 NewItem = data[front]; front =(front+1)%Max; */
	}
}


template<class Type>
void Queue<Type>::enQueue(Type NewItem)
{
	if (IsFullQueue())
	{
		cout<<"栈满!"<<endl;
	}
	else
	{
		data[(rear++)%maxSize]= NewItem;
	}
}

template<class Type>
int Queue<Type>::lengthQueue()
{
	return ((rear - front + maxSize)%maxSize); 
}

int main()
{
	Queue<char>queue;
	char a[4] = {'1','2','3','4'};
	for (int i=0;i<4;i++)
	{
		queue.enQueue(a[i]);
	}
	Queue<char>queue1;
	queue1 = queue;
	char data;
	for (int i=0;i<4;i++)
	{
		queue1.deQueue(data);
		cout<<data<<endl;
	}
	system("pause");
	return 0;
}


 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值