A queue can be implemented using any linear data structure. Here, we'll implement a queue using an array, vector, and LinkedList. A fixed sized queue can be implemented using an array, whereas a dynamic sized queue can be implemented using a vector or LinkedList.
Implementing a queue
Queues with a fixed size
In this implementation, the queue size is fixed and the enqueue() operation throws QueueOverflowException after the queue is full.
Here is the implementation:
class FixedQueue<E> {
private val elements: Array<Any?>
private var size = 0
constructor(capacity: Int) {
this.elements = arrayOfNulls(capacity)
}
fun enqueue(element: E) {
if (size == elements.size) {
throw...