当前位置:网站首页>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
}
}
边栏推荐
- VRRP协议的作用及VRRP+OSPF配置方法
- 3D激光SLAM:LeGO-LOAM---两步优化的帧间里程计及代码分析
- 历史拉链数据处理有人做过吗
- Traceback (most recent call last): File
- 实至名归!九章云极DataCanvas公司荣获智能制造领域多项殊荣
- [华为云在线课程][SQL语法入门][学习笔记]
- 本周四晚19:00知识赋能第4期直播丨OpenHarmony智能家居项目之设备控制实现
- [Output each bit of an integer, from high to low.With and without recursion]
- 机器学习(第一章)—— 特征工程
- C#+WPF 单元测试项目类高级程序员必知必会
猜你喜欢

FR9811S6 SOT-23-6 23V,2A同步降压DC/DC转换器

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

完全背包问题

机器学习(第一章)—— 特征工程

How to use outside the PHP command in the container

像用户体验设计师一样思考

Machine Learning Overview

for in 和 for of的区别

鸿蒙第三次

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!...
随机推荐
使用.NET简单实现一个Redis的高性能克隆版(一)
Advanced use of MySQL database
下午见!2022京东云数据库新品发布会
Depth study of 100 cases - convolution neural network (CNN) to realize the clothing image classification
在线生成接口文档
LP流动性挖矿DAPP系统开发丨流动性挖矿功能原理及说明
「全球数字经济大会」登陆 N 世界,融云提供通信云服务支持
How to retrieve IDC research reports?
通过组策略安装软件和删除用户配置文件
苏州大学:从PostgreSQL到TDengine
白帽黑客与留守儿童破壁对“画”!ISC、中国光华科技基金会、光明网携手启动数字安全元宇宙公益展
MySQL数据库基本使用
Basic using MySQL database
完全背包问题的思路解析
CADEditorX ActiveX 14.1.X
What is the ERC20 token standard?
Simple implementation of a high-performance clone of Redis using .NET (1)
【多线程的相关内容】
QT with OpenGL(Shadow Mapping)(面光源篇)
MapReduce中ETL数据清洗案例