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.
/* Queue
* A Queue is a data structure that allows you to add an element to the end of
* a list and remove the item at the front. A queue follows a "First In First Out"
* system, where the first item to enter the queue is the first to be removed. This
* implementation uses an array to store the queue.
*/

// Functions: enqueue, dequeue, peek, view, length

var Queue = (function () {
  // constructor
  function Queue () {
    // This is the array representation of the queue
    this.queue = []
  }

  // methods
  // Add a value to the end of the queue
  Queue.prototype.enqueue = function (item) {
    this.queue[this.queue.length] = item
  }

  // Removes the value at the front of the queue
  Queue.prototype.dequeue = function () {
    if (this.queue.length === 0) {
      throw new Error('Queue is Empty')
    }

    var result = this.queue[0]
    this.queue.splice(0, 1) // remove the item at position 0 from the array

    return result
  }

  // Return the length of the queue
  Queue.prototype.length = function () {
    return this.queue.length
  }

  // Return the item at the front of the queue
  Queue.prototype.peek = function () {
    return this.queue[0]
  }

  // List all the items in the queue
  Queue.prototype.view = function () {
    console.log(this.queue)
  }

  return Queue
}())

// Implementation
var myQueue = new Queue()

myQueue.enqueue(1)
myQueue.enqueue(5)
myQueue.enqueue(76)
myQueue.enqueue(69)
myQueue.enqueue(32)
myQueue.enqueue(54)

myQueue.view()

console.log('Length: ' + myQueue.length())
console.log('Front item: ' + myQueue.peek())
console.log('Removed ' + myQueue.dequeue() + ' from front.')
console.log('New front item: ' + myQueue.peek())
console.log('Removed ' + myQueue.dequeue() + ' from front.')
console.log('New front item: ' + myQueue.peek())
myQueue.enqueue(55)
console.log('Inserted 55')
console.log('New front item: ' + myQueue.peek())

for (var i = 0; i < 5; i++) {
  myQueue.dequeue()
  myQueue.view()
}

// console.log(myQueue.dequeue()); // throws exception!

LANGUAGE:

DARK MODE: