循环队列的基本操作实现(C++)

(1)建立循环队列并实现判空、入队、出队、取队头元素等基本操作;

(2)在主函数中测试循环队列的基本操作:

要求:
1)输入一个序列入队,然后再显示队列的内容;
2)显示队头元素;
3)出队一个元素后,再显示队列内容。
main.cpp

#include<iostream>
using namespace std;
#include"CircQueue.h"


template<class ElemType>
void Show(const ElemType &e)
{
	cout << e << " ";
}

int main()
{
	int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	CircQueue<int>CQ(10);
	bool T = true;
	int b;
	//入队前判空
	T=CQ.Empty();
	cout << "入队前判空:" << T << endl;
	//入队
	for (int i = 0; i < 9; i++)
		CQ.InQueue(a[i]);
	//显示队列内容
	CQ.Traverse(Show);
	//入队后判空
	T = CQ.Empty();
	cout<< "入队后判空:" << T << endl;
	//显示队头元素
	CQ.GetHead(b);
	cout << "队头元素为:" << b << endl;
	//出队一个元素后显示队列内容
	//出队
	CQ.outQueue();
	//显示队列内容
	CQ.Traverse(Show);

	return 0;
}

CircQueue.h

#pragma once
#include<iostream>
using namespace std;

//循环队列类模板
template<class ElemType>
class CircQueue
{
protected:
	//数据成员
	ElemType* elems;
	int maxSize;
	int front, rear;
	int count;

public:
	CircQueue(int size);
	virtual ~CircQueue();
	int Length()const;
	bool Empty()const;
	void Clear();
	void Traverse(void (*visit)(const ElemType&))const;
	bool outQueue(ElemType &e);
	bool outQueue();
	bool GetHead(ElemType& e)const;
	bool InQueue(const ElemType& e);
};

template<class ElemType>
CircQueue<ElemType>::CircQueue(int size)
{
	maxSize = size;
	elems = new ElemType[maxSize];
	rear = front = 0;
	count = 0;
}

template<class ElemType>
CircQueue<ElemType>::~CircQueue()
{
	delete[]elems;
}

template<class ElemType>
int CircQueue<ElemType>::Length()const
{
	return count;
}

template<class ElemType>
bool CircQueue<ElemType>::Empty()const
{
	return count == 0;
}

template<class ElemType>
void CircQueue<ElemType>::Clear()
{
	rear = front = 0;
	count = 0;
}



template<class ElemType>
void CircQueue<ElemType>::Traverse(void (*visit)(const ElemType&))const
{
	cout << "遍历队列:" << endl;
	for (int temPos = front; temPos != rear; temPos = (temPos + 1) % maxSize)
	{
		(*visit)(elems[temPos]);
	}
	cout << endl;
}

template<class ElemType>
bool CircQueue<ElemType>::outQueue(ElemType& e)
{
	
	if (!Empty())
	{
		e = elems[front];
		front = (front + 1) % maxSize;
		count--;
		return true;
	}
	else
	{
		return false;
	}
}

template<class ElemType>
bool CircQueue<ElemType>::outQueue()
{
	if (!Empty())
	{
		front = (front + 1) % maxSize;
		count--;
		return true;
	}
	else
	{
		return false;
	}
}

template<class ElemType>
bool CircQueue<ElemType>::GetHead(ElemType& e)const
{
	if (!Empty())
	{
		e = elems[front];
		return true;
	}
	else
	{
		return false;
	}
}

template<class ElemType>
bool CircQueue<ElemType>::InQueue(const ElemType& e)
{
	if (count==maxSize)
	{
		return false;
	}
	else
	{
		elems[rear] = e;
		rear = (rear + 1) % maxSize;
		count++;
		return true;
	}
}


  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GT-一二

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值