Implementation of queue using stacks

Run Settings
LanguageJavaScript
Language Version
Run Command
class CrazyQueue { constructor() { this.first = []; this.last = []; } enqueue(value) { const length = this.first.length; for (let i = 0; i < length; i++) { this.last.push(this.first.pop()); } this.last.push(value); return this; } dequeue() { const length = this.last.length; for (let i = 0; i < length; i++) { this.first.push(this.last.pop()); } this.first.pop(); return this; } peek() { if (this.last.length > 0) { return this.last[0]; } return this.first[this.first.length - 1]; } } const myQueue = new CrazyQueue(); myQueue.peek(); myQueue.enqueue('Joy'); myQueue.enqueue('Matt'); myQueue.enqueue('Pavel'); myQueue.peek(); myQueue.dequeue(); myQueue.dequeue(); myQueue.dequeue(); myQueue.peek();
Editor Settings
Theme
Key bindings
Full width
Lines