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

#pragma once
//循环顺序队列
#ifndef My_Head_H
	#define My_Head_H
	#include "G://code/c++/myhead.h"
#endif // !My_Head_H

template <typename ElemType>
class SqQueue
{
public:
	//把队列置空
	void clear();

	//出队列(删除队头结点)
	Status deQueue(ElemType& e);

	//进队列(在队尾加入结点)
	Status enQueue(ElemType e);

	//求队列中结点个数
	int Get_Length();

	//以随机数填充i个结点
	void Random_Fill_Queue(int i);

	//打印所有结点
	void Display_Queue();

	//判断是否为空
	bool Is_Empty();

	//判断是否队列已满
	bool Is_Full();

	SqQueue(int size = 100);
	~SqQueue();

protected:
	int rear;  //队尾指针
	int front; //队首指针
	int Queue_Size;//循环队列最大储存空间
	ElemType* base;


};

template<typename ElemType>
inline void SqQueue<ElemType>::clear()
{
	front = rear;//队首队尾指针相等即队列为空;
}

template<typename ElemType>
Status inline SqQueue<ElemType>::deQueue(ElemType& e)
{
	if (Is_Empty()) return ERROR;
	e = base[front]; //通过参数返回出元素
	front = (front + 1) % Queue_Size;//修改头指针
	return OK;
}

template<typename ElemType>
Status inline SqQueue<ElemType>::enQueue(ElemType e)
{
	if (Is_Full()) return ERROR;
	base[rear] = e;
	rear = (rear + 1) % Queue_Size;
	return OK;
}

template<typename ElemType>
inline int SqQueue<ElemType>::Get_Length()
{
	return (rear - front + Queue_Size) / Queue_Size;
}

template<typename ElemType>
inline void SqQueue<ElemType>::Random_Fill_Queue(int i)
{
	for (int count = 0; count < i; count++)
	{
		enQueue(random(100));
	}
}

template<typename ElemType>
inline void SqQueue<ElemType>::Display_Queue()
{
	cout << "Here is all Queue data:" << endl;
	int p = front;
	while (p!=rear)
	{
		cout << "->" << base[p];
		p = (p + 1) % Queue_Size;
	}
	cout << endl;
}

template<typename ElemType>
inline bool SqQueue<ElemType>::Is_Empty()
{
	return front == rear ? true : false;
}

template<typename ElemType>
inline bool SqQueue<ElemType>::Is_Full()
{
	return (rear + 1) % Queue_Size == front ? true : false;
}

template <typename ElemType>
SqQueue<ElemType>::SqQueue(int size)
{
	base = new ElemType[size];
	front = rear = 0;
	Queue_Size = size;
}

template <typename ElemType>
SqQueue<ElemType>::~SqQueue()
{
	delete[]base;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值