Javascript Queue data structure
Bvnkumar
Posted on April 18, 2022
QUEUE:
Queue is also one type of data structure and works like a stack but it follows a first in first out(FIFO) order.
Example: When you are in a line to pay a bill at the counter, we should follow the sequence like first in first out, queue also works same.
Internal design of queue:
const Queue = function() {
let collections = [];
this.add = function(item) {
return collections.push(item);
}
this.size = function() {
return collections.length;
}
this.isEmpty = function() {
retun(collections.length == 0)
}
this.enqueue = function(item) {
return collections.push(item)
}
this.dequeue = function(item) {
return collections.shift();
}
this.front = function() {
return collections[0];
}
}
let queue = new Queue();
console.log(queue.size())
queue.enqueue("1");
queue.enqueue("2");
console.log(queue.size())
console.log(queue.front());
queue.dequeue();
console.log(queue.size())
Any comments or suggestions are welcome.
💖 💪 🙅 🚩
Bvnkumar
Posted on April 18, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.