当前位置:网站首页>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
}
}
边栏推荐
猜你喜欢
多态详细讲解(简单实现买票系统模拟,覆盖/重定义,多态原理,虚表)
Classical Architecture and Memory Classification of Embedded Software Components
numpy
跨链桥协议 Nomad 遭遇黑客攻击,损失超 1.5 亿美元
Machine Learning (Chapter 1) - Feature Engineering
机器学习(第一章)—— 特征工程
Binary search tree (search binary tree) simulation implementation (there is a recursive version)
The way of programmer architecture practice: how to design a sustainable evolution system architecture?
What is the relationship between The Matrix and 6G?
实至名归!九章云极DataCanvas公司荣获智能制造领域多项殊荣
随机推荐
Generate interface documentation online
complete knapsack problem
Android 技术面试准备(含面试题及答案)
The way of programmer architecture practice: how to design a sustainable evolution system architecture?
"Global Digital Economy Conference" landed in N World, Rongyun provides communication cloud service support
Matplotlib
STM32入门开发 介绍SPI总线、读写W25Q64(FLASH)(硬件+模拟时序)
numpy
Summary of redis basics - data types (strings, lists, sets, hashes, sets)
再谈“雷克萨斯”安全装置失效!安全手册疑点重重,网友:细思极恐
完全背包问题的思路解析
Cross-chain bridge protocol Nomad suffers hacker attack, losing more than $150 million
如何检索IDC研究报告?
数据库一席谈:打造开源的数据生态,支撑产业数字化浪潮
Binary search tree (search binary tree) simulation implementation (there is a recursive version)
Objective - C code analysis of the deep and shallow copy
build --repot
MySQL数据库实战(1)
build --repot
历史拉链数据处理有人做过吗