博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode C++ 622. Design Circular Queue【设计/循环队列】中等
阅读量:2007 次
发布时间:2019-04-28

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

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 3circularQueue.enQueue(1);  // return truecircularQueue.enQueue(2);  // return truecircularQueue.enQueue(3);  // return truecircularQueue.enQueue(4);  // return false, the queue is fullcircularQueue.Rear();  // return 3circularQueue.isFull();  // return truecircularQueue.deQueue();  // return truecircularQueue.enQueue(4);  // return truecircularQueue.Rear();  // return 4

Note:

  • All values will be in the range of [0, 1000] .
  • The number of operations will be in the range of [1, 1000] .
  • Please do not use the built-in Queue library.

题意:设计循环队列实现。 循环队列是一种线性数据结构,其操作表现基于 FIFO(先进先出)原则并且队尾被连接在队首之后以形成一个循环。也被称为"环形缓冲器"。循环队列的一个好处是可以利用这个队列之前用过的空间。在一个普通队列里,一旦一个队列"满了",就不能插入下一个元素,即使在队列前面仍有空间。但是使用循环队列,就能使用这些空间去存储新的值。


思路

感觉LeetCode上的设计题都可以替代平常的数据结构实现课程的学习了。代码如下:

class MyCircularQueue {
private: int *queue; int capacity; int size; int front, rear; //[front, rear]public: /** Initialize your data structure here. Set the size of the queue to be k. */ MyCircularQueue(int k) {
queue = new int[capacity = k]; size = 0; front = 0, rear = -1; } /** Insert an element into the circular queue. Return true if the operation is successful. */ bool enQueue(int value) {
if (isFull()) return false; ++size; rear = (rear + 1) % capacity; queue[rear] = value; return true; } /** Delete an element from the circular queue. Return true if the operation is successful. */ bool deQueue() {
if (isEmpty()) return false; --size; front = (front + 1) % capacity; return true; } /** Get the front item from the queue. */ int Front() {
if (isEmpty()) return -1; return queue[front]; } /** Get the last item from the queue. */ int Rear() {
if (isEmpty()) return -1; return queue[rear]; } /** Checks whether the circular queue is empty or not. */ bool isEmpty() {
return size == 0; } /** Checks whether the circular queue is full or not. */ bool isFull() {
return size == capacity; }};

转载地址:http://mmotf.baihongyu.com/

你可能感兴趣的文章
连锁门店无线通信方案
查看>>
配置Lotus Domino集群视频详解
查看>>
Linux软件万花筒
查看>>
全球开源软件发展趋势分析
查看>>
python 多进程之进程池的操作
查看>>
flask整理之 flask程序中的debug模式
查看>>
比特币,父母这一辈能接受吗?
查看>>
首个聚合器怎么产生的,并运用领域在什么
查看>>
区块链技术应用,最先医疗行业
查看>>
《增长黑客》(肖恩·艾利斯)学习笔记——第二部分 实战
查看>>
python使用HTMLTestRunner查看运行函数
查看>>
linux下安装jenkins+git+python
查看>>
解决uiautomatorviewer中添加xpath的方法
查看>>
jenkins安装提示Please wait while Jenkins is getting ready to work...(Jenkins访问资源慢的问题)
查看>>
性能测试的必要性评估以及评估方法
查看>>
Spark学习——利用Mleap部署spark pipeline模型
查看>>
使用redis实现订阅功能
查看>>
java内存模型
查看>>
volatile关键字
查看>>
Request_继承体系
查看>>