The code implementation of the circular queue

Circular queue

1. Basic understanding of circular queue

Insert picture description here
Insert picture description here

2. Concrete realization

  • Implement circular queue with array
2.1 Join the team
  • Ideas:
  1. Also judge whether it is full
  2. The added element is added to the position of the rear, and then the rear moves to the back. Originally, it only needs to be added, but it is a circular queue. The next one of the last is the subscript 0, so the modulo operation is used;
//入队
    public boolean enQueue(int value) {
    
    
        //1.先判断是否为满
        if(isFull()){
    
    
            return false;
        }
        this.elem[rear] = value;
        rear = (rear+1)%this.elem.length;
        return true;
    }

2.2 Departure
  • Ideas:
  1. First determine whether it is empty
  2. Out directly from the front of the team;
public boolean deQueue() {
    
    
        //1.判断是否为空
        if(isEmpty()){
    
    
            return false;
        }
        front = (front+1)%elem.length;
        return true;
    }

2.3 Get the head element
  • Idea: Because it is implemented with an array, just return the element of the head subscript directly;
public int Front() {
    
    
        //1.判断是否为空
        if(isEmpty()){
    
    
            return -1;
        }
        return elem[front];
    }
2.4 Get tail elements
  • Idea: If rear returns to the previous one at position 0, modulo arithmetic is required; it can be directly subtracted;
//获取队尾
    public int Rear() {
    
    
        //1.判断是否为空
        if(isEmpty()){
    
    
            return -1;
        }
        int index = this.rear==0 ? this.elem.length-1 :  this.rear-1;
        return elem[index];
    }
2.5 Determine whether it is empty
  • Idea: As long as rear == front is empty
//是否为空
    public boolean isEmpty() {
    
    
        if(rear == front){
    
    
            return true;
        }
        return false;
    }
2.6 Determine whether it is full
  • Idea: Just judge whether the next rear is the head element;
public boolean isFull() {
    
    
        if((rear+1)%elem.length == front){
    
    
            return true;
        }
        return false;
    }

3. Complete code

//循环队列(用数组实现)
class MyCircularQueue {
    
    
    public int[] elem;
    public int front;//队头
    public int rear;//队尾

    /** Initialize your data structure here. Set the size of the queue to be k. */
    //构造方法
    public MyCircularQueue(int k) {
    
    
        this.elem = new int[k+1];
    }

    /** Insert an element into the circular queue. Return true if the operation is successful. */
    //入队
    public boolean enQueue(int value) {
    
    
        //1.先判断是否为满
        if(isFull()){
    
    
            return false;
        }
        this.elem[rear] = value;
        rear = (rear+1)%this.elem.length;
        return true;
    }

    /** Delete an element from the circular queue. Return true if the operation is successful. */
    //出队
    public boolean deQueue() {
    
    
        //1.判断是否为空
        if(isEmpty()){
    
    
            return false;
        }
        front = (front+1)%elem.length;
        return true;
    }

    /** Get the front item from the queue. */
    //获取队头元素
    public int Front() {
    
    
        //1.判断是否为空
        if(isEmpty()){
    
    
            return -1;
        }
        return elem[front];
    }

    /** Get the last item from the queue. */
    //获取队尾
    public int Rear() {
    
    
        //1.判断是否为空
        if(isEmpty()){
    
    
            return -1;
        }
        int index = this.rear==0 ? this.elem.length-1 :  this.rear-1;
        return elem[index];
    }

    /** Checks whether the circular queue is empty or not. */
    //是否为空
    public boolean isEmpty() {
    
    
        if(rear == front){
    
    
            return true;
        }
        return false;
    }

    /** Checks whether the circular queue is full or not. */
    //是否为满
    public boolean isFull() {
    
    
        if((rear+1)%elem.length == front){
    
    
            return true;
        }
        return false;
    }
}

public class TestDemo4 {
    
    
    public static void main(String[] args) {
    
    
        MyCircularQueue myCircularQueue = new MyCircularQueue(8);
        myCircularQueue.enQueue(1);
        myCircularQueue.enQueue(2);
        myCircularQueue.enQueue(3);
        myCircularQueue.enQueue(4);
        System.out.println(myCircularQueue.Front());
        System.out.println(myCircularQueue.Rear());
    }
}

Guess you like

Origin blog.csdn.net/qq_45665172/article/details/110208588