Tutorials Logic, IN info@tutorialslogic.com

Queue Data Structure FIFO, Priority Queue

FIFO Invariant

A queue is a first-in, first-out collection. enqueue adds at the rear and dequeue removes from the front. A priority queue is a different abstraction: removal follows priority rather than arrival order.

If a and then b are enqueued and neither is removed, a must leave first. Track both front and rear so enqueue and dequeue remain constant time. Removing index zero from a dynamic array repeatedly is usually O(n) because remaining elements shift.

Circular Array Queue

A fixed buffer can reuse released slots by wrapping indexes modulo capacity. Store the current size or reserve one slot to distinguish full from empty. Define whether the queue rejects, blocks, or grows when full.

Linked Queue and Deque

A linked queue keeps head and tail pointers. Enqueue appends at tail; dequeue removes head; when the last value leaves, both pointers must return to empty. A deque supports insertion and removal at both ends and is useful for monotonic-window algorithms.

Applications and Backpressure

Queues support breadth-first search, task scheduling, message buffering, and producer-consumer boundaries. In production, an unbounded queue converts overload into memory growth and latency. Capacity, rejection, timeout, retry, and visibility are part of the queue contract.

Circular Queue Index Rules

Circular Queue Index Rules
class CircularQueue {
        constructor(capacity) {
          this.items = new Array(capacity);
          this.front = 0;
          this.size = 0;
        }
        enqueue(value) {
          if (this.size === this.items.length) return false;
          const rear = (this.front + this.size) % this.items.length;
          this.items[rear] = value;
          this.size++;
          return true;
        }
        dequeue() {
          if (this.size === 0) return undefined;
          const value = this.items[this.front];
          this.front = (this.front + 1) % this.items.length;
          this.size--;
          return value;
        }
      }

Traverse a Graph Breadth First

The queue processes vertices in discovery order and the visited set prevents repeated work.

Traverse a Graph Breadth First
function bfs(graph, start) {
  const queue = [start], seen = new Set([start]), order = [];
  for (let head = 0; head < queue.length; head++) {
    const node = queue[head];
    order.push(node);
    for (const next of graph[node]) {
      if (!seen.has(next)) { seen.add(next); queue.push(next); }
    }
  }
  return order;
}
console.log(bfs({ A:['B','C'], B:['D'], C:[], D:[] }, 'A').join(' '));
Output
A B C D
  • A head index avoids the O(n) shifting cost of Array.shift().
Before you move on

Queue Data Structure FIFO, Priority Queue Mastery Check

6 checks
  • FIFO order is preserved.
  • Empty and full behavior is explicit.
  • Front and rear wrap correctly.
  • The implementation avoids front-shifting arrays.
  • Capacity and overload policy are documented.
  • Tests cover wraparound and reuse.

Queue Data Structure Questions Learners Ask

A FIFO queue removes the oldest item. A priority queue removes the item with the highest or lowest priority according to its ordering rule.

Browse Free Tutorials

Explore 500+ free tutorials across 20+ languages and frameworks.