当前位置:网站首页>LeetCode——622.设计循环队列
LeetCode——622.设计循环队列
2022-08-03 11:03:00 【Cap07】
package j12;
import java.util.*;
class A {
int x[] = new int[1000];
void addx(int location, int value) {
x[location] = value;
}
}
class MyCircularQueue {
A a = new A();
int length = 0;
int user = 0;
int rear_location = 0;
int front_location = 0;
public MyCircularQueue(int k) {
length = k;
}
public boolean enQueue(int value) {
if (user < length) {
a.x[rear_location] = value;
// a.addx(rear_location,value);
rear_location++;
user++;
return true;
}
else {
return false;
}
}
public boolean deQueue() {
if (length == 0||rear_location==front_location) {
return false;
} else {
user--;
front_location++;
return true;
}
}
public int Front() {
if (length == 0||rear_location==front_location) {
return -1;
} else {
return a.x[front_location];
}
}
public int Rear() {
if (length == 0||rear_location==front_location) {
return -1;
} else {
int temp=rear_location;
temp--;
return a.x[temp];
}
}
public boolean isEmpty() {
if (user == 0) {
return true;
} else {
return false;
}
}
public boolean isFull() {
if(user==length){
return true;
}
else {
return false;
}
}
}
/**
* 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();
*/
public class j13 {
public static void main(String args[]) {
MyCircularQueue circularQueue = new MyCircularQueue(6);
System.out.println(circularQueue.enQueue(6));
System.out.println(circularQueue.Rear());
System.out.println(circularQueue.Rear());
System.out.println(circularQueue.deQueue());
System.out.println(circularQueue.enQueue(5));
System.out.println(circularQueue.Rear());
System.out.println(circularQueue.deQueue());
System.out.println(circularQueue.Front());
System.out.println(circularQueue.deQueue());
System.out.println(circularQueue.deQueue());
System.out.println(circularQueue.deQueue());
// MyCircularQueue circularQueue = new MyCircularQueue(3); // 设置长度为 3
// System.out.println(circularQueue.enQueue(1)); // 返回 true
// System.out.println(circularQueue.enQueue(2)); // 返回 true
// System.out.println(circularQueue.enQueue(3)); // 返回 true
// System.out.println(circularQueue.enQueue(4)); // 返回 false,队列已满
// System.out.println(circularQueue.Rear()); // 返回 3
// System.out.println(circularQueue.isFull()); // 返回 true
// System.out.println(circularQueue.deQueue()); // 返回 true
// System.out.println(circularQueue.enQueue(4)); // 返回 true
// System.out.println(circularQueue.Rear()); // 返回 4
}
}
边栏推荐
- numpy
- 在 Chrome 开发者工具里通过 network 选项模拟网站的离线访问模式
- LyScript 实现对内存堆栈扫描
- ABAB-740新语法
- 【文件IO的简单实现】
- MapReduce中ETL数据清洗案例
- Babbitt | Metaverse daily must-read: Players leave, platforms are shut down, and the digital collection market is gradually cooling down. Where is the future of the industry?...
- 云原生 Dev0ps 实践
- BPMN和DMN基本概念和使用案例
- 实至名归!九章云极DataCanvas公司荣获智能制造领域多项殊荣
猜你喜欢
机器学习概述
本周四晚19:00知识赋能第4期直播丨OpenHarmony智能家居项目之设备控制实现
下午见!2022京东云数据库新品发布会
如何检索IDC研究报告?
LyScript implements memory stack scanning
QT with OpenGL(Shadow Mapping)(面光源篇)
完全背包问题的思路解析
Skills required to be a good architect: How to draw a system architecture that everyone will love?What's the secret?Come and open this article to see it!...
3分钟实现内网穿透(基于ngrok实现)
MySQL数据库高级使用
随机推荐
Advanced use of MySQL database
C - 为什么指针常常初始化为 NULL?
ScrollView嵌套RecyclerView滚动冲突
机器学习(第一章)—— 特征工程
Summary of redis basics - data types (strings, lists, sets, hashes, sets)
Matplotlib
LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之二
面试官:工作两年了,这么简单的算法题你都不会?
LyScript 实现对内存堆栈扫描
【输出一个整数的的每一位,由高到低输出。使用递归和不使用递归】
LyScript implements memory stack scanning
【多线程的相关内容】
redis基础知识总结——数据类型(字符串,列表,集合,哈希,集合)
[Detailed explanation of binary search plus recursive writing method] with all the code
RecyclerView的item高度自适应
Matplotlib
如何通过DBeaver 连接 TDengine?
CADEditorX ActiveX 14.1.X
Polymorphism in detail (simple implementation to buy tickets system simulation, covering/weight definition, principle of polymorphism, virtual table)
Web Server 设置缓存响应字段的一些推荐方案