Stack Test

Run Settings
LanguageJavaScript
Language Version
Run Command
class Node { constructor(value){ this.value = value; this.next = null; } } class Stack { constructor(){ this.top = null; this.bottom = null; this.length = 0 } push(value){ if(!value) return; let newNode = new Node(value); if(this.length === 0) { this.bottom = newNode; this.top = newNode; }else{ newNode.next = this.top this.top = newNode } this.length++; } peek(){ return this.top.value; } pop(){ if(this.length === 0) return null let removedVal = this.top.value; if(this.length === 1){ this.bottom = null; this.top = null; }else{ this.top = this.top.next; } this.length--; return removedVal; } } const stack = new Stack(); stack.push(45) stack.push(50) stack.push(80) stack.push(801) stack.push(8011) stack.push(80112) console.log(stack); console.log(stack.peek()) console.log(stack.pop()) console.log(stack.pop()) console.log(stack.pop()) console.log(stack.pop()) console.log(stack.pop()) console.log(stack.pop()) console.log(stack.pop()) console.log(stack.pop())
Editor Settings
Theme
Key bindings
Full width
Lines