设计循环队列
时间:2020-04-28
本文章向大家介绍设计循环队列,主要包括设计循环队列使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
//循环队列 约定:head = -1且tail=-1时为空 class MyCircularQueue { private: int* elements; int head; int tail; int size; public: /* initialize your data structure here, set the size of the queue to be k*/ MyCircularQueue(int k) { elements = new int[k]; size = k; head = tail = -1; } ~MyCircularQueue() { delete[] elements; } /*insert an element into the circular queue.Return true if the operation is successful*/ bool enQueue(int value) { if (isFull()) return false; if (isEmpty()) head++; tail = (tail + 1) % size; elements[tail] = value; } bool deQueue() { if (isEmpty()) return false; //约定head=-1&&tail=-1为空 if (head == tail) head = tail = -1; else head = (head + 1) % size; return true; } /*get the front the item from the queue*/ int Front() { if (isEmpty()) return -1; return elements[head]; } /*get the last item from the queue*/ int Rear() { if (isEmpty()) return -1; return elements[tail]; } /*check whether the circular queue is empty or not*/ //约定head=-1且tail=-1时为空 bool isEmpty() { return head == -1 && tail == -1; } /*check whether the circular queue is full or not*/ bool isFull() { return (tail + 1) % size == head; } }; /*your MyCircularQueue object will be instantiated and called as such:*/ /* MyCircularQueue *obj = new MyCircularQueue(k); bool param_1 = obj->enQueue(value) bool param_2 = obj->deQueue(value) int param_3 = obj->Front(); int param_4 = obj->Rear(); bool param_5 = obj->isEmpty(); bool param_6 = obj->isFull(); */
原文地址:https://www.cnblogs.com/hujianglang/p/12793536.html
- 用Qt写软件系列五:一个安全防护软件的制作(1)
- Linux文件管理
- 为什么区块链会成为消除数字化营销障碍的解决方案
- TinyOS和Deluge的安装模拟(二)
- Kubernetes的服务网格(第4部分):通过流量切换持续部署
- QTableView表格视图的列宽设置
- OpenProcess打开进程返回错误的问题
- Python标准库01 正则表达式 (re包)
- 剑指OFFER之栈的压入、弹出序列(九度OJ1366)
- Python标准库03 路径与文件 (os.path包, glob包)
- AI人工智能时代已经到来 “北斗即时判”实现纯语音交互
- 剑指OFFER之链表中倒数第k个节点(九度OJ1517)
- 用Qt写软件系列四:定制个性化系统托盘菜单
- Linux简介与厂商版本
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 如何在SAP Spartacus自定义Component里消费数据
- 使用ConfigModule.withConfig替换SAP Spartacus标准Component
- pod 资源对象
- 在Java8的foreach()中使用return/break/continue
- JavaScript 覆盖对象字段级别
- Zabbix5.0 问题记录
- 「查缺补漏」巩固你的Nginx知识体系
- 超级帐本合约调用——fabric SDK Node调用TBaaS的合约
- TBaaS合约调用_fabric SDK Node 不使用Connection Profile
- helm安装、使用、实践
- 浅析asp .net core 中间件
- 玩转jenkins - 在自己的服务器上安装jenkins
- CPU有个禁区,内核权限也无法进入!
- Java bytecodes
- 云原生分布式深度学习初探