当前位置:网站首页>Leetcode - 622 design cycle queue (Design)
Leetcode - 622 design cycle queue (Design)
2022-07-25 15:40:00 【Cute at the age of three @d】


Array to implement the queue

The header pointer points to the previous position of the current header element The tail pointer points to the tail element
class MyCircularQueue {
private int[] queue;// Array of queues
private int size;// How many elements are actually stored in the queue
private int head;// The head pointer
private int tail;// Tail pointer
private int capacity;// Queue fixed size
public MyCircularQueue(int k) {
// The fixed size of the queue is k
this.queue = new int[k];
this.size = 0;
this.head = 0;
this.tail = 0;
this.capacity = k;
}
public boolean enQueue(int value) {
if(isFull())// The queue is full Value cannot be queued
return false;
else{
// Put the value at the end of the queue
queue[tail] = value;
// Queue size plus one
size += 1;
// The tail pointer moves backward
tail = (++tail) % this.capacity;
return true;
}
}
public boolean deQueue() {
// If the queue is empty Failed to value out of the queue
if(isEmpty())
return false;
else{
// The head pointer moves back
head = (++head) % this.capacity;
// Reduce the queue size by one
size -= 1;
return true;
}
}
public int Front() {
if(isEmpty())
return -1;
else
// Return the corresponding value of the header pointer
return queue[head];
}
public int Rear() {
if(isEmpty())
return -1;
else{
// tail It refers to the current location of the data to be saved So the actual last number in the queue is tail The last position of
int rear = (tail - 1 + this.capacity) % this.capacity;
return queue[rear];
}
}
public boolean isEmpty() {
// Judge whether the actual capacity of the queue is 0
return this.size == 0;
}
public boolean isFull() {
// Judge whether the actual number of numbers in the queue is equal to the queue capacity
return this.size == this.capacity;
}
}
/** * Your MyCircularQueue object will be instantiated and called as such: * MyCircularQueue obj = new MyCircularQueue(k); * boolean param_1 = obj.enQueue(value); * boolean param_2 = obj.deQueue(); * int param_3 = obj.Front(); * int param_4 = obj.Rear(); * boolean param_5 = obj.isEmpty(); * boolean param_6 = obj.isFull(); */
边栏推荐
- Pytorch学习笔记--常用函数总结2
- 2021上海市赛-D-卡特兰数变种,dp
- 你准备好脱离“内卷化怪圈”了吗?
- PAT甲级题目目录
- Week303 of leetcode
- See a lot of blinking pictures on apps, especially the member page
- 2019 Zhejiang race c-wrong arrangement, greedy
- matlab 如何保存所有运行后的数据
- Application of C language array in Sanzi chess -- prototype of Queen n problem
- 数据系统分区设计 - 请求路由
猜你喜欢
随机推荐
CF566A-贪心+字典树
IOS interview questions
LeetCode - 380 O(1) 时间插入、删除和获取随机元素 (设计 哈希表+数组)
对this对象的理解
Node learning
GAMES101复习:三维变换
Qtime定义(手工废物利用简单好看)
JVM知识脑图分享
ICPC2021昆明M-暴力+主席树
4PAM在高斯信道与瑞利信道下的基带仿真系统实验
谷歌博客:采用多重游戏决策Transformer训练通用智能体
微信小程序
C#精挑整理知识要点11 委托和事件(建议收藏)
In depth: micro and macro tasks
SQL cultivation manual from scratch - practical part
Find out what happened in the process of new
var、let、const之间的区别
Cf750f1 thinking DP
LeetCode - 362 敲击计数器(设计)
JVM - classloader and parental delegation model







