Implementation of queue using linked list

Run Settings
LanguageJavaScript
Language Version
Run Command
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++; return this } dequeue(){ if(!this.first){ return null } if(this.first===this.last){ this.last=null; } const holdingPointer=this.first; this.first=this.first.next; this.length--; return this; } } const q = new Queue() q.enqueue(4) q.enqueue(5) q.enqueue(6) q.dequeue() q.dequeue() q.dequeue()
Editor Settings
Theme
Key bindings
Full width
Lines