环形队列之C++封装
来源:互联网 发布:php apache 404 编辑:程序博客网 时间:2024/06/14 13:15
一.环形队列
队列是一种常用的数据结构,这种结构保证了数据是按照“先进先出”的原则进行操作的,即最先进去的元素也是最先出来的元素.环形队列是一种特殊的队列结构,保证了元素也是先进先出的,但与一般队列的区别是,他们是环形的,即队列头部的上个元素是队列尾部,通常是容纳元素数固定的一个闭环。
1、普通队列:【空间利用率不高】
2、环形队列:【申请好的空间可以循环利用,较普通队列空间利用率高】
二、环形队列的C++封装实现:
.h文件:
#pragma onceclass CZzcQueue{public:CZzcQueue(int nCapacity); //创建队列virtual ~CZzcQueue(); //销毁队列void ClearQueue(); //判断队列是否为空bool IsQueueEmpty()const; //获取队列的长度bool IsQueueFull()const;int GetQueueLength()const; //获取队列的长度bool InsertItemToQueue(int nItem); //向队列尾中插入元素(int型)bool DeleteItemFromQueue(int &nItem);//删除队列首元素(int型)void QueueTraverse(); //遍历队列private:int* m_pQueue; //队列数组指针。(int型 是队列中存放元素的类型)int m_nQueueLen; //队列中的元素个数int m_nQueueCapacity; //队列的总容量int m_nQueueHead; //队列头的索引int m_nQueueTail; //队列尾的索引};
.cpp文件:
#include "StdAfx.h"#include "ZzcQueue.h"#include <iostream>using namespace std;CZzcQueue::CZzcQueue(int nCapacity){m_nQueueCapacity = nCapacity;m_nQueueHead = 0;m_nQueueTail = 0;m_nQueueLen = 0;//创建队列 //队列中存放的元素为int型,也可为其它复杂的结构体m_pQueue = new int[m_nQueueCapacity];}CZzcQueue::~CZzcQueue(){delete []m_pQueue;m_pQueue = NULL;}void CZzcQueue::ClearQueue(){m_nQueueHead = 0;m_nQueueTail = 0;m_nQueueLen = 0;}bool CZzcQueue::IsQueueEmpty()const{return m_nQueueLen == 0?true:false;}int CZzcQueue::GetQueueLength()const{return m_nQueueLen;}bool CZzcQueue::IsQueueFull()const{if (m_nQueueLen == m_nQueueCapacity){return true;}else return false;}bool CZzcQueue::InsertItemToQueue(int nItem){if (IsQueueFull()){return false;}else{m_pQueue[m_nQueueTail] = nItem;m_nQueueTail++;m_nQueueTail = m_nQueueTail % m_nQueueCapacity;m_nQueueLen++;return true;}}bool CZzcQueue::DeleteItemFromQueue(int &nItem){if (IsQueueEmpty()){return false;} else{nItem = m_pQueue[m_nQueueHead];m_nQueueHead++;m_nQueueHead = m_nQueueHead % m_nQueueCapacity;m_nQueueLen--;return true;}}void CZzcQueue::QueueTraverse(){for (int i = m_nQueueHead;i < m_nQueueLen + m_nQueueHead;i++){cout<<m_pQueue[i%m_nQueueCapacity]<<endl;}}
阅读全文
0 0
- 环形队列之C++封装
- C语言 环形队列
- 【c++】环形队列类模板
- 队列之环形队列基本操作
- 【C/C++】缓冲区设计--环形队列
- C语言实现的环形队列
- 第七周项目一C/C++数据结构之自建算法库——顺序环形队列
- java之环形队列的实现
- 队列小记二之环形结构
- C++之环形队列的实现
- Linux 生产者、消费者之环形队列信号量
- 数据结构之环形队列实现(1)
- 数据结构之环形队列应用(2)
- 环形队列
- 环形队列
- 环形队列
- 环形队列
- 环形队列
- div和span;选择器
- ROS机器人操作系统的安装、配置与初级教程 12
- struts2用注解开发出错一 There is no Action mapped for namespace [/] and action name [zhujie] associated with
- PostMessage
- ROS机器人操作系统的安装、配置与初级教程 13
- 环形队列之C++封装
- 我的JDBC Dao模式链接数据库
- VIVADO 安装教程
- jQuery.fn.extend() 和 jQuery.extend()
- Spark编程指引(四)----共享变量(广播变量和累加器)
- 修改mysql密码
- 计算在1901年1月1日至2000年12月31日间共有多少个星期天落在每月的第一天上
- yii2实用插件总结
- Ubuntu下安装并配置maven