Sitemap

Data Structures and Algorithms(DSA) using JavaScript — Queue using Linked List

2 min readJun 3, 2020

--

Press enter or click to view image in full size
Photo by Melanie Pongratz on Unsplash

A Queue is a linear structure which follows a particular order in which the operations are performed

Get The Coding Bear’s stories in your inbox

Join Medium for free to get updates from this writer.

The order is First In First Out (FIFO)

prerequisite

  • JavaScript Classes
  • Linked List

Applications

  • Servers
  • CPU scheduling
  • Call Center phone systems will use Queues
  • Breadth First search in a Graph

Operations

Enqueue: Adds an item to the queue. If the queue is full, then it is said to be an Overflow condition.
Dequeue: Removes an item from the queue. The items are popped in the same order in which they are pushed. If the queue is empty, then it is said to be an Underflow condition.
peek: Get the front item from queue.
Rear: Get the last item from queue.

Implementation

class Node{
constructor(value) {
this.value = value;
this.next = null;
}
}
class Queue { constructor(){
this.first = null;
this.last = null;
this.length= 0;
}

enqueue(val) {
let newNode = new Node(val);
if (!this.first){
this.first = newNode;
this.last = newNode;
} else {
this.last.next = newNode;
this.last = newNode;
}
return ++this.length;
}
dequeue() {
if (!this.first) return null;
var temp = this.first;
if (this.first === this.last) {
this.last = null;
}
this.first = this.first.next;
this.length--;
return temp.value;
}
peek(){
return this.first;
}
rear(){
return this.last;
}
}

Happy coding 🎉 and Have a nice day🎊

To be Continued…

Don’t forget to applause if you found it helpful😜

--

--

The Coding Bear
The Coding Bear

Written by The Coding Bear

Made with ❤, by Harish Sambasivam