# Queue idea and implementation of a simple case - java version

(schematic diagram) About queues, we should first know the characteristics of queues

1) Queue is a sequential list, which can be realized by array or linked list. This chapter uses array to realize

2) Follow the principle of first in first out, that is, take out the data stored first and take out the data stored later

3) Schematic diagram (above)

So, first create the queue and initialize its properties

```class aQueue{
private int maxSize;//Maximum capacity of the queue
private int rear;//Queue tail
private int front;//Queue header
private int[] arr;//Array itself

public aQueue(int arrMaxSize){
maxSize = arrMaxSize;
front = -1;//Point to the queue header and analyze that front is the previous position pointing to the queue header
rear = -1;//Point to the end of the queue, and point to the data itself at the end of the queue (that is, the last data of the queue)
arr = new int[maxSize];
}
}```

There is a problem → why both front and rear point to - 1? You can understand it in another way. In programming languages, 0 is usually used as the first element of a memory. For example, the java array arr represents the first number. Therefore, both the head and tail of the queue point to - 1, indicating that there is no data

After initializing the queue, we'll discuss several important methods

```    //Determine whether the queue is empty
public boolean isEmpty(){
return front == rear;
}
//Determine whether the queue is full
public boolean isFull(){
return rear== (maxSize - 1);
}
//Join the team
public void setQueue(int value){
if (isFull()){
System.out.println("The queue is full, cannot join!");
return;
}
rear++;
arr[rear] = value;
//You can also use one step to realize arr[++rear] = value;
}
//Out of the team
public int outQueue(){
if (isEmpty()){
throw new RuntimeException("The queue is empty and data cannot be retrieved!");
}
return arr[++front];
}
```

Here are a few questions to explain

1. Why is the condition for judging to be empty front == rear?

According to the top schematic diagram, when joining the team, the end of the team (rear) points to the current element. Suppose there are 4 elements joining the team and 4 elements leaving the team. When all of them leave the team, it happens that rear == front.

2. The condition for judging whether the team is full is rear = maxSize - 1

The rear points to the current element, and the queue is from 0 to maxSize -1. When the rear points to maxSize-1, it points to the last element of the queue. At this time, the queue is full

3. Join the team to achieve rear++, arr[rear] = value;

In the queue joining method, first judge whether it is empty. If not, execute rear++, arr[rear] = value; First let the tail pointer + 1, and then store the data. In other words, when joining the team, you should store the new data to the next position where the current rear points, so you should first add 1 to the rear, and then store the data

Problem analysis and optimization

1) At present, the array cannot be used once, which does not achieve the effect of reuse

2) This array uses the algorithm. The process is a ring queue. The key of the algorithm is to take the module. The specific problems will be updated in the next chapter

Finally, attach a simple small source code

```package com.queue;

public class ArrayQueue {
public static void main(String[] args) {
aQueue a = new aQueue(5);
a.setQueue(10);
a.setQueue(20);
a.setQueue(30);
a.setQueue(40);
a.setQueue(50);
System.out.println(a.outQueue());
System.out.println(a.outQueue());
}
}
class aQueue{
private int maxSize;//Maximum capacity of the queue
private int rear;//Queue tail
private int front;//Queue header
private int[] arr;//Array itself
public aQueue(int arrMaxSize){
maxSize = arrMaxSize;
front = -1;//Point to the queue header and analyze that front is the previous position pointing to the queue header
rear = -1;//Point to the end of the queue, and point to the data itself at the end of the queue (that is, the last data of the queue)
arr = new int[maxSize];
}
//Determine whether the queue is empty
public boolean isEmpty(){
return front == rear;
}
//Determine whether the queue is full
public boolean isFull(){
return rear== (maxSize - 1);
}
//Join the team
public void setQueue(int value){
if (isFull()){
System.out.println("The queue is full, cannot join!");
return;
}
arr[++rear] = value;
}
//Out of the team
public int outQueue(){
if (isEmpty()){
throw new RuntimeException("The queue is empty and data cannot be retrieved!");
}
return arr[++front];
}