Stack Implementation using Singly Linked List

Run Settings
LanguageJavaScript
Language Version
Run Command
class Node{ constructor(val){ this.value=val; this.next=null; } } class Stack{ constructor(){ this.top=null; this.bottom=null; this.length=0; } push(val){ if (this.length ===0){ this.top = new Node(val); this.bottom = this.top; }else{ let currentTop = this.top; this.top = new Node(val); this.top.next =currentTop; } this.length++; return this; } pop(){ let currentTop = this.top; this.top = currentTop.next; this.length--; if (this.length===0){ this.bottom = null; } return this; } peek(){ return this.top; } } const myStack = new Stack(); myStack.push('google'); myStack.push('udemy'); myStack.push('linkedin'); console.log(myStack.pop()); console.log(myStack.pop()); console.log(myStack.pop());
Editor Settings
Theme
Key bindings
Full width
Lines