Queue Algorithm

The Queue Using Array Algorithm is an implementation of the queue data structure using an array as its underlying storage. A queue is a linear data structure that follows the First-In-First-Out (FIFO) principle, where elements are inserted at the rear end and removed from the front end. In this algorithm, two pointers, front and rear, are used to keep track of the first and last elements in the queue, respectively. When an element is enqueued, it is added at the rear end of the array, and the rear pointer is incremented. When an element is dequeued, it is removed from the front end, and the front pointer is incremented. The algorithm also includes a check for overflow and underflow conditions to ensure that the queue operates within the bounds of the array. One of the main advantages of implementing a queue using an array is its simplicity and ease of understanding. The algorithm provides clear rules for enqueueing and dequeueing elements and is easy to implement in most programming languages. However, there are some drawbacks to this approach. The primary disadvantage is the issue of array resizing, as a fixed-sized array may lead to overflow if the queue grows beyond its capacity. To overcome this limitation, a circular queue can be employed, where the front and rear pointers wrap around the array, effectively reusing the freed spaces from dequeued elements. Another issue is inefficient memory utilization, as dequeued elements leave empty spaces in the array that are not utilized. Despite these drawbacks, the Queue Using Array Algorithm serves as a useful and straightforward introduction to the concept of queues and their implementation.
package org.gs.queue

/** Immutable interface for hidden Queue implementation
  *
	* This copies a example from Programming in Scala instead of translating from Algorthms
  * I added an isEmpty method
  * @tparam A elements are generic
  * @tparam A
  * @see [[https://booksites.artima.com/programming_in_scala_2ed/examples/type-parameterization/Queues5.scala]]
  * @see [[https://algs4.cs.princeton.edu/13stacks/Queue.java.html]]
  * @author Scala translation by Gary Struthers from Java by Robert Sedgewick and Kevin Wayne.
  */
trait Queue[A] {
  def head: A
  def tail: Queue[A]
  def enqueue(x: A): Queue[A]
  def isEmpty(): Boolean
}

object Queue {

/** @param xs a variable number of objects of type A
  * @return an initialized Queue companion object
  * @see QueueSuite for usage
  */
def apply[A](xs: A*): Queue[A] = new QueueImpl[A](xs.toList, Nil)

  /** To Enqueue in constant time prepend to the trailing list
   *
   * To Dequeue in constant time take the head of the leading list. If leading is empty, reverse
   * trailing and assign it to head. The cost of reversing is amortized. After taking head, tail is
   * the remaining Queue
   *
   * @tparam A elements are generic
   */
  private class QueueImpl[A] (
    val leading: List[A],
    val trailing: List[A]) extends Queue[A] {

    private def mirror() = if (leading.isEmpty) new QueueImpl(trailing.reverse, Nil) else this

    def head(): A = mirror.leading.head

    def tail(): QueueImpl[A] = {
      val q = mirror
      new QueueImpl(q.leading.tail, q.trailing)
    }

    def enqueue(x: A): Queue[A] = new QueueImpl(leading, x :: trailing)

    def isEmpty(): Boolean = leading.isEmpty && trailing.isEmpty
  }
}

LANGUAGE:

DARK MODE: