|
| 1 | +package com.thealgorithms.datastructures.queues; |
| 2 | + |
| 3 | +/** |
| 4 | + * This class implements a Queue data structure using an array. |
| 5 | + * A queue is a first-in-first-out (FIFO) data structure where elements are |
| 6 | + * added to the rear and removed from the front. |
| 7 | + * |
| 8 | + * Note: This implementation is not thread-safe. |
| 9 | + */ |
| 10 | +public final class Queue<T> { |
| 11 | + |
| 12 | + private static final int DEFAULT_CAPACITY = 10; |
| 13 | + |
| 14 | + private final int maxSize; |
| 15 | + private final Object[] queueArray; |
| 16 | + private int front; |
| 17 | + private int rear; |
| 18 | + private int nItems; |
| 19 | + |
| 20 | + /** |
| 21 | + * Initializes a queue with a default capacity. |
| 22 | + */ |
| 23 | + public Queue() { |
| 24 | + this(DEFAULT_CAPACITY); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Constructor to initialize a queue with a specified capacity. |
| 29 | + * |
| 30 | + * @param capacity The initial size of the queue. |
| 31 | + * @throws IllegalArgumentException if the capacity is less than or equal to zero. |
| 32 | + */ |
| 33 | + public Queue(int capacity) { |
| 34 | + if (capacity <= 0) { |
| 35 | + throw new IllegalArgumentException("Queue capacity must be greater than 0"); |
| 36 | + } |
| 37 | + this.maxSize = capacity; |
| 38 | + this.queueArray = new Object[capacity]; |
| 39 | + this.front = 0; |
| 40 | + this.rear = -1; |
| 41 | + this.nItems = 0; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Inserts an element at the rear of the queue. |
| 46 | + * |
| 47 | + * @param element Element to be added. |
| 48 | + * @return True if the element was added successfully, false if the queue is full. |
| 49 | + */ |
| 50 | + public boolean insert(T element) { |
| 51 | + if (isFull()) { |
| 52 | + return false; |
| 53 | + } |
| 54 | + rear = (rear + 1) % maxSize; |
| 55 | + queueArray[rear] = element; |
| 56 | + nItems++; |
| 57 | + return true; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Removes and returns the element from the front of the queue. |
| 62 | + * |
| 63 | + * @return The element removed from the front of the queue. |
| 64 | + * @throws IllegalStateException if the queue is empty. |
| 65 | + */ |
| 66 | + @SuppressWarnings("unchecked") |
| 67 | + public T remove() { |
| 68 | + if (isEmpty()) { |
| 69 | + throw new IllegalStateException("Queue is empty, cannot remove element"); |
| 70 | + } |
| 71 | + T removedElement = (T) queueArray[front]; |
| 72 | + queueArray[front] = null; // Optional: Clear the reference for garbage collection |
| 73 | + front = (front + 1) % maxSize; |
| 74 | + nItems--; |
| 75 | + return removedElement; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Checks the element at the front of the queue without removing it. |
| 80 | + * |
| 81 | + * @return Element at the front of the queue. |
| 82 | + * @throws IllegalStateException if the queue is empty. |
| 83 | + */ |
| 84 | + @SuppressWarnings("unchecked") |
| 85 | + public T peekFront() { |
| 86 | + if (isEmpty()) { |
| 87 | + throw new IllegalStateException("Queue is empty, cannot peek front"); |
| 88 | + } |
| 89 | + return (T) queueArray[front]; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Checks the element at the rear of the queue without removing it. |
| 94 | + * |
| 95 | + * @return Element at the rear of the queue. |
| 96 | + * @throws IllegalStateException if the queue is empty. |
| 97 | + */ |
| 98 | + @SuppressWarnings("unchecked") |
| 99 | + public T peekRear() { |
| 100 | + if (isEmpty()) { |
| 101 | + throw new IllegalStateException("Queue is empty, cannot peek rear"); |
| 102 | + } |
| 103 | + return (T) queueArray[rear]; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Returns true if the queue is empty. |
| 108 | + * |
| 109 | + * @return True if the queue is empty. |
| 110 | + */ |
| 111 | + public boolean isEmpty() { |
| 112 | + return nItems == 0; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Returns true if the queue is full. |
| 117 | + * |
| 118 | + * @return True if the queue is full. |
| 119 | + */ |
| 120 | + public boolean isFull() { |
| 121 | + return nItems == maxSize; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Returns the number of elements currently in the queue. |
| 126 | + * |
| 127 | + * @return Number of elements in the queue. |
| 128 | + */ |
| 129 | + public int getSize() { |
| 130 | + return nItems; |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Returns a string representation of the queue. |
| 135 | + * |
| 136 | + * @return String representation of the queue. |
| 137 | + */ |
| 138 | + @Override |
| 139 | + public String toString() { |
| 140 | + if (isEmpty()) { |
| 141 | + return "[]"; |
| 142 | + } |
| 143 | + StringBuilder sb = new StringBuilder(); |
| 144 | + sb.append("["); |
| 145 | + for (int i = 0; i < nItems; i++) { |
| 146 | + int index = (front + i) % maxSize; |
| 147 | + sb.append(queueArray[index]).append(", "); |
| 148 | + } |
| 149 | + sb.setLength(sb.length() - 2); // Remove the last comma and space |
| 150 | + sb.append("]"); |
| 151 | + return sb.toString(); |
| 152 | + } |
| 153 | +} |
0 commit comments