techStackGuru

An overview of queue data structures


In a queue, removing elements is based on the First-In-First-Out (FIFO) principle, so the first element added is also the first to be removed. In computer science, queues are used in a wide variety of applications, including operating systems, network programming, and data processing.

stack-1

Skeleton of a stack class

class Queue {
  constructor() {
     this.items = [];
  }
				
// The functions that need to be implemented
// enqueue(item)
// dequeue()
// peek()
// isEmpty()
}

Queue data structure properties

  • The queue operates according to the First-In-First-Out principle, meaning that the first item added to the queue will be the first item removed from the queue.
  • When a program is executed, queues can be dynamically expanded or contracted based on the needs of the program.
  • A queue is a linear data structure, meaning that it stores elements sequentially.A sequence element is added to the back and a sequence element is removed from the front.
  • The elements of queues are limited in access.It is only possible to access elements from the front and back of the queue, which means they cannot be accessed from the middle without first removing the elements in front.
  • There are typically two pointers used in queue implementations, a front pointer and a rear pointer. Initially, the front pointer identifies the first element in the queue, and at the end, it identifies the last element. As elements are added to or removed from the queue, pointers are updated.
  • Queue data structures operations

    Declaring

    class Queue { 
    constructor() 
      { 
        this.items = []; 
      } 
    } 

    Enqueue

    An element is pushed to the top of the stack with this operation.

    enqueue(element) {
       this.items.push(element);
    } 

    Dequeue

    An element at the top of a stack is removed using this operation.

    dequeue() {
       this.items.shift();
    } 

    Peek

    In peek, the top element of a stack is returned without being removed.

    peek() {
       this.items[0];
    }

    IsEmpty

    Checks whether the stack is empty by using IsEmpty.

    isEmpty() {
       this.items.length == 0;
    }
    

    There are many applications for stack data structures in computer science, including:

  • Process scheduling
  • Resource allocation
  • Data processing
  • Network programming
  • Customer service
  • Example

    class Queue {
        constructor() {
          this.items = {};
          this.headIndex = 0;
          this.tailIndex = 0;
        }
        enqueue(item) {
          this.items[this.tailIndex] = item;
          this.tailIndex++;
        }
        dequeue() {
          const item = this.items[this.headIndex];
          delete this.items[this.headIndex];
          this.headIndex++;
          return item;
        }
        peek() {
          return this.items[this.headIndex];
        }
        get length() {
          return this.tailIndex - this.headIndex;
        }
      } 
      const queue = new Queue();
      
      queue.enqueue(1);
      console.log(queue); // Queue { items: { '0': 1 }, headIndex: 0, tailIndex: 1 }
      
      queue.enqueue(2);
      console.log(queue); // Queue { items: { '0': 1, '1': 2 }, headIndex: 0, tailIndex: 2 }
      
      queue.enqueue(3);
      console.log(queue); // Queue { items: { '0': 1, '1': 2, '2': 3 }, headIndex: 0, tailIndex: 3 }
      
      console.log(queue.dequeue()); // 1
      
      console.log(queue); // Queue { items: { '1': 2, '2': 3 }, headIndex: 1, tailIndex: 3 }
      
      //length
      console.log(queue.length) // 2
      
      //isEmpty
      console.log(queue.length == 0) // false 
      
      //Peek
      console.log(queue.peek())  // 2