c++实现基本循环队列操作
来源:互联网 发布:java 热部署 编辑:程序博客网 时间:2024/06/04 01:02
在慕课网上学习的c++循环队列,自己也写了个实现代码。
头文件:
#ifndef MYQUEUE_H#define MYQUEUE_Hclass MyQueue{public: MyQueue(int queueCapacity); //InitQueue(&q) 创建队列 virtual ~MyQueue(); //DestroyQueue(&Q)销毁队列 void clearQueue(); //ClearQueue(&Q) 清空队列 bool QueueEmpty() const; //QueueEmpty(&Q) 判空队列 int QueueLength() const; //QueueLength(&Q) 判断队列长度 bool EnQueue(int element); //EnQueue(&Q, element)新元素入队 bool QueueFull() const; bool DeQueue(int &element); //DeQueue(&Q,&element)首元素出队 void QueueTraverse(); //QueueTraverse(Q,visit())遍历队列 private: int *m_pQueue; //队列数组指针 int m_iQueueLen; //队列元素个数 int m_iQueueCapacity; //队列数组容量 int m_iHead; int m_iTail;};#endif
#include<iostream>#include "MyQueue.h"using namespace std;MyQueue::MyQueue(int queueCapacity){ m_iQueueCapacity=queueCapacity; clearQueue(); m_pQueue=new int[m_iQueueCapacity];}MyQueue::~MyQueue(){ delete []m_pQueue; m_pQueue=NULL;}void MyQueue::clearQueue(){ m_iHead=0; m_iTail=0; m_iQueueLen=0;} bool MyQueue::QueueEmpty() const{ return m_iQueueLen==0?true:false;}int MyQueue::QueueLength() const{ return m_iQueueLen;}bool MyQueue::QueueFull() const{ return m_iQueueLen==m_iQueueCapacity?true:false;}bool MyQueue::EnQueue(int element){ if (QueueFull()) { cout<<"false"<<endl; return false; } else{ m_pQueue[m_iTail]=element; m_iTail++; m_iTail=m_iTail%m_iQueueCapacity; //循环队列,这一步很关键 m_iQueueLen++; return true; }}bool MyQueue::DeQueue(int &element){ if (QueueEmpty()) { cout<<"empty"; return false; } element=m_pQueue[m_iHead]; m_iHead++; m_iHead=m_iHead%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; }}int main(){ MyQueue *p=new MyQueue(4); p->EnQueue(15); p->EnQueue(16); p->EnQueue(18); p->EnQueue(20); p->EnQueue(35); p->QueueTraverse(); cout<<endl; int n=0; p->DeQueue(n); p->QueueTraverse(); cout<<endl; p->clearQueue(); p->DeQueue(n); return 0;}
代码的实现
阅读全文
0 0
- 顺序队列(循环队列)基本操作实现 C语言
- 循环队列的基本操作实现
- 实现循环队列的基本操作
- 数据结构-循环队列的基本实现操作
- c++实现基本循环队列操作
- 循环队列基本操作
- 循环队列基本操作
- 循环队列基本操作
- C语言实现循环队列基本操作(初始化、判断队空、入队、出队)
- 循环队列基本实现
- c实现队列的基本操作
- C语言线性表循环队列的基本操作
- 数据结构循环队列的基本操作(C语言)
- 循环队列操作实现
- 循环队列的基本操作之代码实现_legend
- 循环队列的基本操作
- 循环队列的基本操作
- 循环队列的基本操作
- Macbook上打开多个终端的方法
- CSI相关知识
- 每日一学(七)Android 动画设计概述
- html 生成markdown编辑器
- Hive Metastore 启动成功又失败
- c++实现基本循环队列操作
- UITableView上添加UITextView动态行高
- JSP基础知识
- kelude的使用小记
- index name too long
- ASP.NET五大核心对象解析
- laravel5.4安装redis扩展包
- Thinking in java-23 类的加载、链接和初始化
- Q118:PBRT-V3材质及其对应的反射模型(笔记)