How to use an array to implement a queue in Java language?

        Here's the thing, you're allowed to use array to realize a queure which has a main feature, that says "first in and first out."  Linked list is not permitted to use in this situation.

        In this context ,  we must understand what we should do, there is a list for all main features below.

  1.  An array, which has a fixed capacity, can save all elements.
  2.  Enqueue, an element  is permitted to enter the queue when is not full.
  3.  Dequeue, an element is allowed to leave the queue when is not empty.

        now, we can have a clear mind about the things we need. 

public class Queue {
    private int maxSize;
    private int[] queueArray;
    private int front;
    private int rear;
    private int currentSize;

    public Queue(int size) {
        // init maxSize
        maxSize = size;
        queueArray = new int[maxSize];
        // points the head of queue, it should plus 1 while a element wants to leave.
        front = 0;
        // rear points the tail of queue, it should plus 1 while a element's comming.
        rear = -1;
        currentSize = 0;
    }

    public void enqueue(int item) {
        if (isFull()) {
            System.out.println("Queue is full. Cannot enqueue item: " + item);
            return;
        }
        rear = (rear + 1) % maxSize;
        queueArray[rear] = item;
        currentSize++;
    }

    public int dequeue() {
        if (isEmpty()) {
            System.out.println("Queue is empty. Cannot dequeue item.");
            return -1;
        }
        int removedItem = queueArray[front];
        front = (front + 1) % maxSize;
        currentSize--;
        return removedItem;
    }

    public int peek() {
        if (isEmpty()) {
            System.out.println("Queue is empty. Cannot peek item.");
            return -1;
        }
        return queueArray[front];
    }

    public boolean isEmpty() {
        return currentSize == 0;
    }

    public boolean isFull() {
        return currentSize == maxSize;
    }

    public int size() {
        return currentSize;
    }
}

        In this implementation, we use array to store the elments of the queue, the "front" and "rear" variables keep track of the indices of the front and rear element perspectively.  The "currentSize" keeps track of the number of elements in queue.

       The "enqueue" method adds an item to  the rear of the queue, the "dequeue" method removes an item from the front of the queue,  The "peek" method returns the item at the front of the queue without removing it.

        The "isEmpty" method is used to check if the queue is empty, the "isFull" method checks if the queue is full,respectively.

        Besides, I use "%" to calculate the index of front  and rear to avoid the index goes outside of the array,  additionaly, it can help reuse the previous space while we prepare to save or remove elements again and again.

Q&A

        Q: Why use "front" and "rear" variables to track the  indices of front and real element, what if we use only "front" variable ? 

        A: If we use only variable "front" to track two sides of elements at the sometime, something bad, which we will reach a  delimma where  we need to remove mutiple elements from rear or insert several elements from front, would happen, because we can't use only one varible to manipulate two sides at the same time,that's the difference from the implementation of stack.

        Q: As we know, the array has a fixed capacity, how to reuse the used space recycled by system? 

        A:   We can use  "%" to calculate the indices,once the index reach the maxSize, we can start it from the first index again.

猜你喜欢

转载自blog.csdn.net/qq_33036061/article/details/132145028