博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
环形队列的c++实现
阅读量:4356 次
发布时间:2019-06-07

本文共 2524 字,大约阅读时间需要 8 分钟。

队列是一种先进先出的线性表,队尾入队存储,队头出队读取。普通队列在数据出队列后,使用过的地址空间依然没有被释放,产生了很大的浪费。环形队列可是使数据地址限定在某个范围内,重复使用。

实现一个环形队列,基本的功能有

1 #ifndef MyQueue_h 2 #define MyQueue_h 3  4 class MyQueue 5 { 6 public: 7     MyQueue(int queueCapacity);     //创建队列 8     ~MyQueue();                     //销毁队列 9     void ClearQueue();              //清空队列10     bool QueueFull() const;         //队列判满11     bool QueueEmpty() const;        //队列判空12     int QueueLength();              //队列长度13     bool EnQueue(int element);      //新元素入队14     bool DeQueue(int &element);     //首元素出队15     void QueueTraverse();           //遍历队列16 private:17     int *_pQueue;                   //队列指针18     int _iQueueLen;                 //队列长度19     int _iQueueCapacity;            //队列容量20     int _iHead;                     //队列头21     int _iTail;                     //队列尾22 }23 24 #endif /* MyQueue_h */

构造函数和析构函数实现创建、销毁队列并确定队列容量

1 MyQueue::MyQueue(int queueCapacity) 2 { 3     _iQueueCapacity=queueCapacity; 4     _pQueue=new int[_iQueueCapacity]; 5     ClearQueue(); 6 } 7  8 MyQueue::~MyQueue() 9 {10     delete[] _pQueue;11     _pQueue=NULL;12 }

创建队列时应该队头,队尾和队列长度都置为零,所以直接使用了作用相同的清空队列函数

1 void MyQueue::ClearQueue()2 {3     _iHead=0;4     _iTail=0;5     _iQueueLen=0;6 }

队列判空和判满用于判断是否能读取和插入数据

1 bool MyQueue::QueueFull() const 2 { 3     if(_iQueueLen==_iQueueCapacity) 4     { 5         return true; 6     } 7     else 8     { 9         return false;10     }11 }12 13 bool MyQueue::QueueEmpty() const14 {15     if(_iQueueLen==0)16     {17         return true;18     }19     else{20         return false;21     }22 }

在定义队列类时,队列长度写为了私有类型,所以如果有需要,则须用公有类型的QueueLength()函数得到

1 int MyQueue::QueueLength()2 {3     return _iQueueLen;4 }

环形队列中,存和取类似,需要注意的是环形问题,比如当队头的位置大于队列容量时,需要通过对队列容量取余得到正确的队头地址,队尾类似。同时伴随队列长度的加减。

1 bool MyQueue::EnQueue(int element) 2 { 3     if(QueueFull()) 4     { 5         return false; 6     } 7     else 8     { 9         _pQueue[_iTail]=element;10         _iTail=++_iTail%_iQueueCapacity;11         _iQueueLen++;12         return true;13     }14 }15 16 bool MyQueue::DeQueue(int &element)17 {18     if(QueueEmpty())19     {20         return false;21     }22     else23     {24         element=_pQueue[_iHead];25         _iHead=++_iHead%_iQueueCapacity;26         _iQueueLen--;27         return true;28     }29 }

遍历队列时,可以用for循环从头依次输出,注意定义循环变量i时,i的起始在队头,循环次数为队列长度,所以终止在队列长度+队头。输出时的地址也应该如存取时一样,用循环变量对队列容量取余得到正确的地址。

1 void MyQueue::QueueTraverse() 2 { 3     using namespace std; 4      5     cout<

 

转载于:https://www.cnblogs.com/Bird-of-Paradise/p/6362846.html

你可能感兴趣的文章
C#获取枚举描述
查看>>
.NET (C#) Internals: ASP.NET 应用程序与页面生命周期(意译)
查看>>
值语义与对象语义
查看>>
查找(二叉排序树)
查看>>
iphone UI 开发教程
查看>>
17.10.24 数据最水的一次考试
查看>>
python_SMTP and POP3
查看>>
lambda匿名函数
查看>>
js常用方法
查看>>
建造者模式
查看>>
Spring入门教程:通过MyEclipse开发第一个Spring项目
查看>>
【转】你可能不知道的Shell
查看>>
廖雪峰Java1-2程序基础-1基本结构
查看>>
golang下的grpc
查看>>
1. 自动化运维系列之Cobbler自动装机
查看>>
ASP.NET MVC Model绑定(二)
查看>>
一步一步写算法(之hash表)
查看>>
漫谈并发编程(一) - 并发简单介绍
查看>>
JDBC连接MySQL数据库及演示样例
查看>>
Beta 冲刺(1/7)
查看>>