Using array to realize circular queue (java version)

Keywords: Java

It is realized by taking the mold

The ideas are as follows:

  1. Front refers to the first element of the queue, that is to say, arr[front] is the first element of the queue
    Initial value of front = 0
  2. The rear points to the next position of the last element of the queue, because you want to make a space as a convention
    Initial value of real = 0
  3. When the queue is full, the condition is (rear + 1)% maxsize = = front [Full]
  4. If the queue is empty, real = = front is empty
  5. When we analyze this way, the number of valid data in the queue (rear + maxsize - front)% maxsize
  6. Self drawing is easy to understand
    Code implementation:
package queue;

import java.util.Scanner;

public class CircleQueue {

    public static void main(String[] args) {
        // Create a circular queue
        CircleArray queue = new CircleArray(4); //Description setting 4, the maximum effective data of its queue is 3
        char key = ' '; // Receive user input
        Scanner scanner = new Scanner(System.in);//
        boolean loop = true;
        // Output a menu
        while (loop) {
            System.out.println("s(show): Display queue");
            System.out.println("e(exit): Exit procedure");
            System.out.println("a(add): Add data to queue");
            System.out.println("g(get): Get data from queue");
            System.out.println("h(head): View the data of the queue header");
            key = scanner.next().charAt(0);// Receive a character
            switch (key) {
                case 's':
                    queue.showQueue();
                    break;
                case 'a':
                    System.out.println("Output a number");
                    int value = scanner.nextInt();
                    queue.addQueue(value);
                    break;
                case 'g': // Remove data
                    try {
                        int res = queue.getQueue();
                        System.out.printf("The extracted data is%d\n", res);
                    } catch (Exception e) {
                        // TODO: handle exception
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h': // View the data of the queue header
                    try {
                        int res = queue.headQueue();
                        System.out.printf("The data of the queue header is%d\n", res);
                    } catch (Exception e) {
                        // TODO: handle exception
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e': // Sign out
                    scanner.close();
                    loop = false;
                    break;
                default:
                    break;
            }
        }
        System.out.println("Program exit~~");
    }

}


class CircleArray {
    private int maxSize; // Represents the maximum capacity of an array
    //The meaning of the front variable is adjusted: front refers to the first element of the queue, that is to say, arr[front] is the first element of the queue
    //Initial value of front = 0
    private int front;
    //The meaning of the real variable is adjusted to point to the next position of the last element of the queue, because you want to make a space as a convention
    //Initial value of real = 0
    private int rear; // Queue tail
    private int[] arr; // This data is used to store data and simulate queues

    public CircleArray(int arrMaxSize) {
        maxSize = arrMaxSize;
        arr = new int[maxSize];
    }

    // Determine whether the queue is full
    public boolean isFull() {
        return (rear  + 1) % maxSize == front;
    }

    // Judge whether the queue is empty
    public boolean isEmpty() {
        return rear == front;
    }

    // Add data to queue
    public void addQueue(int n) {
        // Determine whether the queue is full
        if (isFull()) {
            System.out.println("The queue is full, unable to add data~");
            return;
        }
        //Add data directly
        arr[rear] = n;
        //Move rear, we must consider taking mould here
        rear = (rear + 1) % maxSize;
    }

    // Get the data of the queue and get out of the queue
    public int getQueue() {
        // Judge whether the queue is empty
        if (isEmpty()) {
            // By throwing an exception
            throw new RuntimeException("Queue is empty, unable to get data");
        }
        // Here we need to analyze that front is the first element pointing to the queue
        // 1. First, keep the value corresponding to front to a temporary variable
        // 2. Move the front backward and consider taking the mold
        // 3. Return the temporarily saved variables
        int value = arr[front];
        front = (front + 1) % maxSize;
        return value;

    }

    // Show all data for the queue
    public void showQueue() {
        // ergodic
        if (isEmpty()) {
            System.out.println("Queue empty, no data~~");
            return;
        }
        // Idea: start from front and traverse how many elements
        // Use one's brains
        for (int i = front; i < front + size() ; i++) {
            System.out.printf("arr[%d]=%d\n", i % maxSize, arr[i % maxSize]);
        }
    }

    // Find the number of valid data in the current queue
    public int size() {
        // rear = 2
        // front = 1
        // maxSize = 3
        return (rear + maxSize - front) % maxSize;
    }

    // Display the header data of the queue. Note that the data is not retrieved
    public int headQueue() {
        // judge
        if (isEmpty()) {
            throw new RuntimeException("Queue empty, no data~~");
        }
        return arr[front];
    }
}
Published 6 original articles, won praise 1, visited 146
Private letter follow

Posted by speedy33417 on Wed, 04 Mar 2020 07:10:17 -0800