Design your implementation of the circular queue. The circular queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position to make a circle. It is also called "Ring Buffer".

One of the benefits of the circular queue is that we can make use of the spaces in front of the queue. In a normal queue, once the queue becomes full, we cannot insert the next element even if there is a space in front of the queue. But using the circular queue, we can use the space to store new values.

Your implementation should support following operations:

  • MyCircularQueue(k): Constructor, set the size of the queue to be k.
  • Front: Get the front item from the queue. If the queue is empty, return -1.
  • Rear: Get the last item from the queue. If the queue is empty, return -1.
  • enQueue(value): Insert an element into the circular queue. Return true if the operation is successful.
  • deQueue(): Delete an element from the circular queue. Return true if the operation is successful.
  • isEmpty(): Checks whether the circular queue is empty or not.
  • isFull(): Checks whether the circular queue is full or not.

Example:

MyCircularQueue circularQueue = new MyCircularQueue(3); // set the size to be 3
circularQueue.enQueue(1);  // return true
circularQueue.enQueue(2);  // return true
circularQueue.enQueue(3);  // return true
circularQueue.enQueue(4);  // return false, the queue is full
circularQueue.Rear();  // return 3
circularQueue.isFull();  // return true
circularQueue.deQueue();  // return true
circularQueue.enQueue(4);  // return true
circularQueue.Rear();  // return 4
 class MyCircularQueue {
final int[] a;
int front, rear = -, len = ; public MyCircularQueue(int k) {
a = new int[k];
} public boolean enQueue(int val) {
if (!isFull()) {
rear = (rear + ) % a.length;
a[rear] = val;
len++;
return true;
} else {
return false;
}
} public boolean deQueue() {
if (!isEmpty()) {
front = (front + ) % a.length;
len--;
return true;
} else
return false;
} public int Front() {
return isEmpty() ? - : a[front];
} public int Rear() {
return isEmpty() ? - : a[rear];
} public boolean isEmpty() {
return len == ;
} public boolean isFull() {
return len == a.length;
}
}

Design Circular Queue的更多相关文章

  1. LeetCode 622:设计循环队列 Design Circular Queue

    LeetCode 622:设计循环队列 Design Circular Queue 首先来看看队列这种数据结构: 队列:先入先出的数据结构 在 FIFO 数据结构中,将首先处理添加到队列中的第一个元素 ...

  2. [Swift]LeetCode622. 设计循环队列 | Design Circular Queue

    Design your implementation of the circular queue. The circular queue is a linear data structure in w ...

  3. [LeetCode] Design Circular Queue 设计环形队列

    Design your implementation of the circular queue. The circular queue is a linear data structure in w ...

  4. LeetCode 622. Design Circular Queue

    原题链接在这里:https://leetcode.com/problems/design-circular-queue/ 题目: Design your implementation of the c ...

  5. [LeetCode] 622.Design Circular Queue 设计环形队列

    Design your implementation of the circular queue. The circular queue is a linear data structure in w ...

  6. 【leetcode】622. Design Circular Queue

    题目如下: Design your implementation of the circular queue. The circular queue is a linear data structur ...

  7. C#LeetCode刷题之#622-设计循环队列​​​​​​​(Design Circular Queue)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4126 访问. 设计你的循环队列实现. 循环队列是一种线性数据结构 ...

  8. 【LeetCode】622. Design Circular Queue 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 用直的代替弯的 数组循环利用 日期 题目地址:htt ...

  9. Leetcode622.Design Circular Queue设计循环队列

    设计你的循环队列实现. 循环队列是一种线性数据结构,其操作表现基于 FIFO(先进先出)原则并且队尾被连接在队首之后以形成一个循环.它也被称为"环形缓冲器". 循环队列的一个好处是 ...

随机推荐

  1. BZOJ 2982 combination 脑子+组合数学

    可以发现,整个数列构成一个树形结构,并且是个完全二叉堆(小根堆). 并且这个堆的形态在给定$n$后是固定的,第1个位置上显然只能放1. 对子树的根来说,他自己是所分得的数集中最小的那个,所以从剩下$s ...

  2. JavaScript实现 简易 堆

    有两个原始操作用于保证插入或删除节点以后堆是一个有效的最大堆或者最小堆: shiftUp(): 如果一个节点比它的父节点大(最大堆)或者小(最小堆),那么需要将它同父节点交换位置.这样是这个节点在数组 ...

  3. js 获取当前focus 的 input 元素

    document.querySelector("#pro-code").focus(); console.log("focus:" + document.act ...

  4. plotly绘图

    import plotly.plotly as plt import plotly.offline as pltoff from plotly.graph_objs import * # 生成折线图 ...

  5. 【线性代数】2-7:转置与变换(Transposes and Permutation)

    title: [线性代数]2-7:转置与变换(Transposes and Permutation) toc: true categories: Mathematic Linear Algebra d ...

  6. 【CUDA 基础】4.4 核函数可达到的带宽

    title: [CUDA 基础]4.4 核函数可达到的带宽 categories: - CUDA - Freshman tags: - 带宽 - 吞吐量 - 矩阵转置 toc: true date: ...

  7. 牛券Cow Coupons

    USACO12FEB 久违的奶牛题. 题意: FJ准备买一些新奶牛,市场上有 $ N $ 头奶牛 $ (1 \leq N \leq 50000) $ ,第i头奶牛价格为 $ P_i (1 \leq P ...

  8. iReport 3.7.6 jasperreport 生成PDF汉字不显示问题的解决

    1.下载iTextAsian.jar 下载地址:https://www.oschina.net/action/code/download?code=51668&id=75706 2.在Irep ...

  9. CISCO实验记录一:路由器基本配置

    一.路由器基本配置要求 1.设置路由器名为:hehe 2.设置特权模式下password为ccna,secret为ccnp,vty线路密码为ccie 3.所有明文密码都加密 二.路由器基本配置命令 1 ...

  10. 在windows系统搭建Flutter开发环境

    开发环境搭建(Windows) 系统要求 设置FLutter镜像(非必须) 获取Flutter SDK Android开发环境设置 安装Flutter插件 系统要求 在Windows上要安装并运行Fl ...