当前位置:网站首页>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
}
}
边栏推荐
- OS层面包重组失败过高,数据库层面gc lost 频繁
- 试题G:单词分析 ← 第十一届蓝桥杯大赛第二场省赛赛题
- Simple implementation of a high-performance clone of Redis using .NET (1)
- 智能合约是什么?
- The way of programmer architecture practice: how to design a sustainable evolution system architecture?
- 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?...
- 多态详细讲解(简单实现买票系统模拟,覆盖/重定义,多态原理,虚表)
- [Star Project] Little Hat Plane Battle (9)
- Question G: Word Analysis ← Questions for the second provincial competition of the 11th Blue Bridge Cup Competition
- Fastjson反序列化
猜你喜欢
随机推荐
MATLAB programming and application 2.7 Structural data and unit data
ABAB-740新语法
深度学习经典网络 -- Inception系列(稀疏结构)
【JS 逆向百例】某网站加速乐 Cookie 混淆逆向详解
What is the relationship between The Matrix and 6G?
直播弱网优化
QT with OpenGL(HDR)
再谈“雷克萨斯”安全装置失效!安全手册疑点重重,网友:细思极恐
C#/VB.NET 从PDF中提取表格
下午见!2022京东云数据库新品发布会
面试一面
【TypeScript】为什么要选择 TypeScript?
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?...
试题G:单词分析 ← 第十一届蓝桥杯大赛第二场省赛赛题
Matplotlib
Spinner文字显示不全解决办法
【Star项目】小帽飞机大战(九)
JS快速高效开发技巧指南(持续更新)
全新的Uber App设计
成为优秀架构师必备技能:怎样才能画出让所有人赞不绝口的系统架构图?秘诀是什么?快来打开这篇文章看看吧!...









