数据结构 环形队列的实现

class MyQueue {
public:
	MyQueue(int queueCapacity); //创建队列
	virtual ~MyQueue();	//销毁队列
	void ClearQueue(); //清空队列
	bool QueueEmpty() const; //判断队列是否为空
	bool QueueFull() const;	//判断队列是否为满
	int Queuelength() const; //队列长度
	bool EnQueue(int element); //新元素入队
	bool DeQueue(int& element); //首元素出队
	void QueueTraverse(); //遍历队列
private:
	int* m_pQueue; //队列数组指针
	int m_iQueueLen; //队列元素个数
	int m_iQueueCapacity; //队列数组容量
	int m_iHead;
	int m_iTail;
};
MyQueue::MyQueue(int queueCapacity)
{
	this->m_iQueueCapacity = queueCapacity;
	this->m_pQueue = new int[m_iQueueCapacity];
	ClearQueue();
}
MyQueue::~MyQueue()
{
	delete[] m_pQueue;
	m_pQueue = NULL;
}
void MyQueue::ClearQueue()
{
	this->m_iHead = 0;
	this->m_iTail = 0;
	this->m_iQueueLen = 0;
}
bool MyQueue::QueueEmpty() const
{
	return m_iQueueLen == 0 ? true : false;
}
bool MyQueue::QueueFull() const
{
	return m_iQueueLen == m_iQueueCapacity ? true : false;
}
int MyQueue::Queuelength() const
{
	return m_iQueueLen;
}
bool MyQueue::EnQueue(int element)
{
	if (QueueFull()) return false;
	this->m_pQueue[m_iTail] = element;
	m_iTail = (m_iTail + 1) % m_iQueueCapacity;
	m_iQueueLen++;
	return true;
}
bool MyQueue::DeQueue(int& element)
{
	if (QueueEmpty())	return false;
	element = m_pQueue[m_iHead];
	m_iHead = (m_iHead + 1) % m_iQueueCapacity;
	m_iQueueLen--;
	return true;
}
void MyQueue::QueueTraverse()
{
	for (int i = m_iHead; i < m_iQueueLen+m_iHead; i++)
		cout << m_pQueue[i % m_iQueueCapacity] << endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值