Stack & Queue

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; } peek(){ return this.top; } push(value){ const newNode=new Node(value); if(this.length===0){ this.top=newNode; this.bottom=newNode; } else{ const holdingPointer=this.top; this.top=this.newNode; this.top.next=holdingPointer; } this.length++; return this; } pop(){ if(!this.top){ return null; } if(this.top===this.bottom){ this.bottom=null; } else{ const holdingPointer=this.top; this.top=this.top.next; this.length--; } return this; } } const myStack=new Stack(); myStack.push('google'); myStack.push('udemy'); myStack.push('discord'); myStack.pop(); myStack.peek();
class Node{ constructor(value){ this.value=value; this.next=null; } } class Queue{ constructor(){ this.first=null; this.last=null; this.length=0; } 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.lenth++; 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; } peek(){ return this.first(); } } const myQueue=new Queue(); myQueue.peek();
Editor Settings
Theme
Key bindings
Full width
Lines