Created
January 31, 2021 14:08
-
-
Save naveenrawat51/bc142a66a9bb0fae923c6acdb016880b to your computer and use it in GitHub Desktop.
Queue implementation in Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node { | |
constructor(value) { | |
this.value = value; | |
this.next = null; | |
} | |
} | |
class Queue { | |
constructor() { | |
this.first = null; | |
this.last = null; | |
this.length = 0; | |
} | |
peek() { | |
return this.first; | |
} | |
enqueue(value) { | |
const newNode = new Node(value); | |
if (this.length === 0) { | |
this.first = newNode; | |
this.last = newNode; | |
} else { | |
this.last.next = newNode; | |
this.last = newNode; | |
} | |
this.length += 1; | |
return this; | |
} | |
dequeue() { | |
if (!this.first) { | |
return null; | |
} | |
if (this.first === this.last) { | |
this.last = null; | |
return null; | |
} | |
this.first = this.first.next; | |
this.length -= 1; | |
return this; | |
} | |
} | |
const queue = new Queue(); | |
queue.enqueue(1); | |
queue.enqueue(2); | |
queue.enqueue(3); | |
queue.enqueue(4); | |
console.log(queue.enqueue(5)); | |
console.log(queue.dequeue()); | |
console.log(queue.dequeue()); | |
console.log(queue.peek()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment