当前位置:网站首页>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
}
}
边栏推荐
猜你喜欢

SAP 电商云 Spartacus UI 的 External Routes 设计明细
![LeetCode 899 有序队列[字典序] HERODING的LeetCode之路](/img/95/1b63cfb25b9e0802666114f089fcb8.png)
LeetCode 899 有序队列[字典序] HERODING的LeetCode之路

用于发票处理的 DocuWare,摆脱纸张和数据输入的束缚,自动处理所有收到的发票

深度学习经典网络 -- Inception系列(稀疏结构)

巴比特 | 元宇宙每日必读:玩家离场,平台关停,数字藏品市场正逐渐降温,行业的未来究竟在哪里?...

创建C UDR时,指定的HANDLESNULLS的作用是什么?

本周四晚19:00知识赋能第4期直播丨OpenHarmony智能家居项目之设备控制实现

for in 和 for of的区别

程序员架构修炼之道:如何设计出可持续演进的系统架构?

"Global Digital Economy Conference" landed in N World, Rongyun provides communication cloud service support
随机推荐
Fastjson反序列化
[Bubble sort and odd-even sorting]
全新的Uber App设计
像用户体验设计师一样思考
在 Chrome 开发者工具里通过 network 选项模拟网站的离线访问模式
鸿蒙第三次
【无标题】函数,对象,方法的区别
【二分查找详解外加递归写法】附有全部代码
numpy
LyScript 实现对内存堆栈扫描
Realize 2d characters move left and right while jumping
Machines need tokens more than people
苏州大学:从PostgreSQL到TDengine
LP流动性挖矿DAPP系统开发丨流动性挖矿功能原理及说明
代码分析Objective-C中的深拷贝与浅拷贝
跨链桥协议 Nomad 遭遇黑客攻击,损失超 1.5 亿美元
LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之二
Simple implementation of a high-performance clone of Redis using .NET (1)
【TypeScript】为什么要选择 TypeScript?
The way of programmer architecture practice: how to design a sustainable evolution system architecture?