Queue 循环队列代码 java(Queue loop queue code java)

 1 package Queue;
 2 
 3 public class Queue {
 4     private int maxSize;
 5     private int front;
 6     private int rear;
 7     private int[] arr;
 8 
 9     public Queue(int maxSize){
10         this.maxSize=maxSize;
11         arr=new int[maxSize];
12     }
13 
14     public void init(){
15         front=0;
16         rear=0;
17     }
18 
19     public boolean isFull(){
20         return (rear+1)%maxSize==front;
21     }
22 
23     public boolean isEmpty(){
24         return rear==front;
25     }
26 
27     public void add(int value){
28         if(isFull())
29             System.out.println("isFull");
30         else{
31             arr[rear]=value;
32             rear=(rear+1)%maxSize;
33             System.out.println("add: "+value);
34         }
35     }
36 
37     public void delete(){
38         if(isEmpty())
39             System.out.println("isEmpty");
40         else{
41             System.out.println("delete: "+arr[front]);
42             front=(front+1)%maxSize;
43         }
44     }
45 
46     public void printQueue(){
47         if(isEmpty()){
48             System.out.println("isFull");
49         }else{
50             System.out.print("getQueue: ");
51             for (int i = (front+1)%maxSize; i <= rear; i=(i+1)%maxSize) {
52                 System.out.print(arr[i]+" ");
53             }
54             System.out.println();
55         }
56     }
57 
58     public int getHead(){
59         if(isEmpty()){
60             System.out.println("isEmpty");
61             return 0;
62         }
63         return arr[front];
64     }
65 }

在循环队列代码中:

front作为队列头部,也就是数组尾部。

rear作为队列尾部,也就是数组头部。

循环队列中具体判满条件为:数组长度等于maxSize-1,(有一个单位作为约定,数组无法被填满,即当数组长度等于maxSize-1时,就默认此队列满了)

————————
 1 package Queue;
 2 
 3 public class Queue {
 4     private int maxSize;
 5     private int front;
 6     private int rear;
 7     private int[] arr;
 8 
 9     public Queue(int maxSize){
10         this.maxSize=maxSize;
11         arr=new int[maxSize];
12     }
13 
14     public void init(){
15         front=0;
16         rear=0;
17     }
18 
19     public boolean isFull(){
20         return (rear+1)%maxSize==front;
21     }
22 
23     public boolean isEmpty(){
24         return rear==front;
25     }
26 
27     public void add(int value){
28         if(isFull())
29             System.out.println("isFull");
30         else{
31             arr[rear]=value;
32             rear=(rear+1)%maxSize;
33             System.out.println("add: "+value);
34         }
35     }
36 
37     public void delete(){
38         if(isEmpty())
39             System.out.println("isEmpty");
40         else{
41             System.out.println("delete: "+arr[front]);
42             front=(front+1)%maxSize;
43         }
44     }
45 
46     public void printQueue(){
47         if(isEmpty()){
48             System.out.println("isFull");
49         }else{
50             System.out.print("getQueue: ");
51             for (int i = (front+1)%maxSize; i <= rear; i=(i+1)%maxSize) {
52                 System.out.print(arr[i]+" ");
53             }
54             System.out.println();
55         }
56     }
57 
58     public int getHead(){
59         if(isEmpty()){
60             System.out.println("isEmpty");
61             return 0;
62         }
63         return arr[front];
64     }
65 }

In the circular queue code:

Front is the head of the queue, that is, the tail of the array.

Rear is the tail of the queue, that is, the head of the array.

The specific full criteria in the circular queue are: the array length is equal to maxsize-1, (there is a unit as the Convention, and the array cannot be filled, that is, when the array length is equal to maxsize-1, the queue is full by default)