LeetCode笔记 622. 设计循环队列

作者:神秘网友 发布时间:2020-09-22 07:03:59

LeetCode笔记 622. 设计循环队列

LeetCode笔记 622. 设计循环队列

LeetCode笔记 622. 设计循环队列

class MyCircularQueue {
private:
	vector <int> data;
	int head;
	int tail;
	int count;
	int size;

public:
	/** Initialize your data structure here. Set the size of the queue to be k. */
	MyCircularQueue(int k) {
		data.resize(k);
		head = 0;
		tail = -1;
		count = 0;
		size = k;
	}

	/** Insert an element into the circular queue. Return true if the operation is successful. */
	bool enQueue(int value) {
		if (isFull()){
			return false;
		}
			
		tail = (tail + 1) % size;
		data[tail] = value;
		count++;
		return true;
	}

	/** Delete an element from the circular queue. Return true if the operation is successful. */
	bool deQueue() {
		if (isEmpty()){
			return false;
		}

		head++;
		count--;
		return true;
	}

	/** Get the front item from the queue. */
	int Front() {
		if (isEmpty())
		{
			return -1;
		}
		return data[head];
	}

	/** Get the last item from the queue. */
	int Rear() {
		if (tail < 0 && tail >= size)
		{
			return -1;
		}
		if (isEmpty())
		{
			return -1;
		}
		return data[tail];
	}

	/** Checks whether the circular queue is empty or not. */
	bool isEmpty() {
		return (count == 0);
	}

	/** Checks whether the circular queue is full or not. */
	bool isFull() {
		return (count == size);
	}
};

LeetCode笔记 622. 设计循环队列相关教程

  1. LeetCode 环形链表
  2. RabbmitMQ学习笔记-RabbitMQ集群架构模式-主备模式
  3. 服了阿里巴巴架构大牛首发SpringCloud笔记GitHub已标星81.6k
  4. 数据结构算法学习笔记——贪心法
  5. JavaScript学习笔记(三十五)——Promise
  6. 力扣笔记-#1431. 拥有最多糖果的孩子
  7. LeetCode | 58.最后一个单词的长度(C语言版)
  8. 孙Cloud的C语言学习小笔记(随时更新)