DEV Community

Saravana Kumar Putta Selvaraj
Saravana Kumar Putta Selvaraj

Posted on

2

JS:DS - Queue Data Structure in JavaScript

The Second Article on the JS:DS series. In this write up we are going to see the Queue Data Structure implementation in javascript and we use arrays for that.

For Stack Data Structure in JS. Please read in the below post.

What is Queue?

The queue is a data structure to store the data in the order of insertion where the item gets inserted at the first into the queue will be the first one to be removed.

In shorter terms First In First Out(FIFO).

How are we going to implement it?

The following are the methods we are going to have in Queue DS.

initialize — Initialize the storage and queue size
enqueue — push item to the queue
dequeue — remove the first pushed item from the queue
getQueueSize — Get the current queue size

initialize

  1. @parmas — size — Queue Size
  2. storage — an array to store the items of the queue DS
  3. currentQueueSize — to track the size of the queue
  4. queueSize — application users can set the predefined size of the queue
class Queue {
  constructor(size) {
    this.storage = [];
    this.currentqueueSize = 0;
    this.queueSize = size ? size : 10;
  }
}
Enter fullscreen mode Exit fullscreen mode

enqueue - Add items to the queue

  1. Check the current size of the queue is less than the actual queue size.
  2. If the above condition gets a pass, increase the current queue size by 1, and push the item to the Queue.
  3. Else throw the error saying that the queue is full!
class Queue {
  constructor(size) {
    this.storage = [];
    this.currentqueueSize = 0;
    this.queueSize = size ? size : 10;
  }
  enqueue(item) {
    if (this.currentqueueSize < this.queueSize) {
      this.queueSize++;
      this.storage.push(item);
    } else {
      throw 'Cannot enqueu. Since the Queue is full!'
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

dequeue - Remove items from the queue

  1. Check the current size of the queue is greater than 0.
  2. If the above condition gets a pass, decrease the current queue size by 1, and remove the item from the first position in an array using shift().
  3. Else throw the error saying that the queue is empty!
class Queue {
  constructor(size) {
    this.storage = [];
    this.currentqueueSize = 0;
    this.queueSize = size ? size : 10;
  }
  dequeue() {
    if (this.currentqueueSize > 0) {
      this.currentqueueSize--;
      return this.storage.shift();
    }
    throw 'Cannot dequeue. Since the Queue is empty!'
  }
}
Enter fullscreen mode Exit fullscreen mode

getQueueSize

Return the current Queue size from the initialization part

class Queue {
  constructor(size) {
    this.storage = [];
    this.currentqueueSize = 0;
    this.queueSize = size ? size : 10;
  }
  getQueueSize() {
    return this.currentqueueSize;
  }
}
Enter fullscreen mode Exit fullscreen mode

Complete code in below gist

class Queue {
/*
Initialization
@parmas - size - Queue Size
storage - an array to store the items of the queue DS
currentQueueSize - to track the size of the queue
queueSize - application users can set the predefined size for the queue
*/
constructor(size) {
this.storage = [];
this.currentqueueSize = 0;
this.queueSize = size ? size : 10;
}
/*
enqueue - Add items to the queue
1. Check the current size of the queue is less than the actual queue size.
2. If above condition gets pass, increase the current queue size by 1 and push the item to queue.
3. Else throw the error saying that the queue is full!
*/
enqueue(item) {
if (this.currentqueueSize < this.queueSize) {
this.queueSize++;
this.storage.push(item);
} else {
throw 'Cannot enqueu. Since the Queue is full!'
}
}
/*
dequeue - Remove items from the queue
1. Check the current size of the queue is greater than 0.
2. If above condition gets pass, decrease the current queue size by 1 and remove the item from the first position in array using shift().
3. Else throw the error saying that the queue is empty!
*/
dequeue() {
if (this.currentqueueSize > 0) {
this.currentqueueSize--;
return this.storage.shift();
}
throw 'Cannot dequeue. Since the Queue is empty!'
}
/*
getQueueSize - return the current queue size
*/
getQueueSize() {
return this.currentqueueSize;
}
}
let queue1 = new Queue(10);
/* Access queue methods as
queue1.enqueue(item),
queue1.dequeue()
queue1.getQueueSize()
*/
console.log(queue1);
view raw Queue.js hosted with ❤ by GitHub

This article is made with ❤️ and am always thankful for the dev community around us!

Please follow me to get to know about latest write ups.

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay