c++实现数据结构七 顺序循环队列

template<typename Type> class SeqQueue
{
private:
	int rear;   //指向队尾
	int front;  //指向队头
	int count;  //队列当前的元素个数
	int MaxSize;  //队列的最大元素个数
	Type *pelements; //指向队列元素的指针
public:
	SeqQueue(int sz): rear(0),front(0),count(0),MaxSize(sz)
	{
		pelements = new Type[sz];
		if(pelements == NULL)
		{
			cout<<"Application error"<<endl;
			exit(1);
		}
	}
	~SeqQueue()
	{
		delete [] pelements;
	}
public:
	void MakeEmpty();
	bool IsEmpty();
	bool IsFull();
	bool Append(const Type item);
	Type Delete();
	Type Get();
	void Print();
};

template<typename Type>
void SeqQueue<Type>::MakeEmpty()
{
	this->front = 0;
	this->rear = 0;
	this->count = 0;
}

template<typename Type>
bool SeqQueue<Type>::IsEmpty()
{
	return count == 0;
}

template<typename Type>
bool SeqQueue<Type>::IsFull()
{
	return count == MaxSize;
}

template<typename Type>
bool SeqQueue<Type>::Append(const Type item)
{
	if(count == MaxSize)
	{
		cout<<"the queue has been full"<<endl;
		return false;
	}
	pelements[rear] = item;
	rear = (rear+1) % MaxSize;
	count++;
	return true;
}

template<typename Type>
Type SeqQueue<Type>::Delete()
{
	if(count == 0)
	{
		cout<<"the queue has been empty"<<endl;
		exit(1);
	}
	Type item = pelements[front];
	front = (front+1) % MaxSize;
	count--;
	return item;
}

template<typename Type>
Type SeqQueue<Type>::Get()
{
	if( count == 0 )
	{
		cout<<"the queue has been empty"<<endl;
		exit(1);
	}
	return pelements[front];
}

template<typename Type>
void SeqQueue<Type>::Print()
{
	cout<<"font";
	for(int i=0; i<count; ++i)
	{
		cout<<"-->"<<pelements[ (front+i) % MaxSize ];
	}
	cout<<"-->rear"<<endl;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值