Queue : Implementation with Linked List

Run Settings
LanguageJavaScript
Language Version
Run Command
class Node { constructor(value){ this.value = value; this.next = null; } } class Queue { constructor(){ this.top = null; this.bottom = null; this.length = 0 } peek(){ return this.top.value } enqueue(value){ if(!value) return const newNode = new Node(value) if(this.length === 0){ this.top = newNode this.bottom = newNode }else{ this.bottom.next = newNode; this.bottom = newNode; } this.length++ return this } empty(){ return this.length === 0 } dequeue(){ if(this.length === 0) return if(this.length === 1){ this.bottom = null; } let currentItem = this.top this.top = this.top.next; this.length--; return currentItem.value } } const queue = new Queue(); queue.enqueue("1") console.log(queue) queue.enqueue("2") console.log(queue) console.log(queue.dequeue()) queue.enqueue("5") console.log(queue.dequeue()) console.log(queue.peek())
Editor Settings
Theme
Key bindings
Full width
Lines