Queues(Linked List)

Run Settings
LanguageJavaScript
Language Version
Run Command
class Node { constructor(val, next=null) { this.val = val; this.next = next; } } class Queue { constructor() { this.first = null; this.last = null; this.length = 0; } // O(1) peek(){ if(this.length === 0) return null; return this.first.val; } // O(1) enqueue(val) { const newNode = new Node(val); if(this.length === 0) { this.first = newNode; this.last = newNode; }else{ this.last.next = newNode; this.last = newNode; } this.length++; return this; } // O(1) dequeue() { if(this.first === null) return null; this.first = this.first.next; this.length--; return this; } } const myQueue = new Queue(); myQueue.enqueue('Kholid'); myQueue.enqueue('Idris'); myQueue.enqueue('Davud'); myQueue.dequeue(); myQueue.dequeue(); myQueue.dequeue(); console.log(myQueue.peek()); // FIFO // Enqueue dequeue/peek // Davud --> Idris --> Kholid -----> School // //
Editor Settings
Theme
Key bindings
Full width
Lines