- 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
- }
- }