[Data structure] Circular queue of queue

public class LoopQueue<T> {

    private int DEFAULT_SIZE = 10;
    // save the length of the array
    private int capacity;
    // Define an array to hold the elements of the circular queue
    private Object[] elementData;
    // Save the current number of elements in the circular queue
    private int front = 0;
    private int rear = 0;

    // Create empty circular queue with default array length
    public LoopQueue() {
        capacity = DEFAULT_SIZE;
        elementData = new Object[capacity];
    }

    // create a circular queue with an initializer element
    public LoopQueue(T element) {
        this();
        elementData[0] = element;
        rear++;
    }

    /**
     * Create a circular queue with an array of specified length
     *
     * @param element specifies the first element in the circular queue
     * @param initSize specifies the length of the underlying array of the circular queue
     */
    public LoopQueue(T element, int initSize) {
        this.capacity = initSize;
        elementData = new Object[capacity];
        elementData[0] = element;
        rear++;
    }

    // Check if the circular queue is empty
    public boolean empty() {
        // rear == front and the element at rear is null
        return rear == front && elementData[rear] == null;
    }

    //Get the size of the circular queue
    public int length() {
        if (empty()) {
            return 0;
        } else {
            return rear > front ? rear - front : capacity - (front - rear);
        }
    }

    // insert into the queue
    public void add(T element) {
        if (rear == front && elementData[front] != null) {
            throw new IndexOutOfBoundsException("Queue full exception");
        }
        elementData[rear++] = element;
        // If rear is over, turn around
        rear = rear == capacity ? 0 : rear;
    }

    // remove queue
    public T remove() {
        if (empty()) {
            throw new IndexOutOfBoundsException("empty queue exception");
        }
        // retain the value of the element on the rear side of the queue
        T oldValue = (T) elementData[front];
        // Release the element on the rear side of the queue
        elementData[front++] = null;
        // If the front has ended, turn the head
        front = front == capacity ? 0 : front;
        return oldValue;
    }

    // Return the top element of the queue, but do not delete the top element of the queue
    public T element() {
        if (empty()) {
            throw new IndexOutOfBoundsException("empty queue exception");
        }
        return (T) elementData[front];
    }

    // clear the circular queue
    public void clear() {
        // Assign all elements of the underlying array to null
        Arrays.fill(elementData, null);
        front = 0;
        rear = 0;
    }

    public String toString() {
        if (empty()) {
            return "[]";
        } else {
            // If front < rear, the valid element is the element between front and rear
            if (front < rear) {
                StringBuilder sb = new StringBuilder("[");
                for (int i = front; i < rear; i++) {
                    sb.append(elementData[i].toString() + ", ");
                }
                int len = sb.length();
                return sb.delete(len - 2, len).append("]").toString();
            }
            // If front >= rear, valid elements are between front -> capacity, 0 -> front
            else {
                StringBuilder sb = new StringBuilder("[");
                for (int i = front; i < capacity; i++) {
                    sb.append(elementData[i].toString() + ", ");
                }
                for (int i = 0; i < rear; i++) {
                    sb.append(elementData[i].toString() + ", ");
                }
                int len = sb.length();
                return sb.delete(len - 2, len).append("]").toString();
            }
        }
    }
}
public class LoopQueueTest {
    public static void main(String[] args) {
        LoopQueue<String> queue = new LoopQueue<String>("aaaa", 3);
        // add element
        queue.add("bbbb");
        queue.add("cccc");
        System.out.println("Initialize element list: " + queue.toString());
        queue.remove();
        System.out.println("Delete the last element list: " + queue);
        queue.add("dddd");
        System.out.println("Add another element list: " + queue);
        System.out.println("Length when the queue is full: " + queue.length());
        queue.remove();
        queue.add("eeee");
        System.out.println("Elements in the circular queue: " + queue);
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324431428&siteId=291194637