You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"use strict";constNode=require("./Node.js");classStack{constructor(){this.top=null;this.length=0;}isEmpty(){if(this.top===null){returntrue;}else{returnfalse;}}push(value){constnewNode=newNode(value);if(this.isEmpty){this.top=newNode;this.length++;}else{newNode.next=this.top;this.top=newNode;this.length++;}}pop(){if(this.isEmpty()){console.log("empty stack");returnfalse;}consttemp=this.top;this.top=this.top.next;temp.next=null;this.length--;returntemp.value;}peek(){if(this.isEmpty()){return"sorry stack is empty";}returnthis.top.value;}}module.exports=Stack;
Queue
constNode=require("./Node.js");classQueue{constructor(){this.front=null;this.rear=null;this.length=0;}isEmpty(){if(this.front===null||this.length===0){returntrue;}else{returnfalse;}}enqueue(value){constnewNode=newNode(value);if(this.isEmpty()){this.front=newNode;this.rear=newNode;this.length++;}else{this.rear.next=newNode;this.rear=newNode;this.length++;}}dequeue(){if(this.isEmpty()){return"sorry queue is empty";}consttemp=this.front;this.front=this.front.next;temp.next=null;this.length--;returntemp.value;}peek(){if(this.isEmpty()){returnnull;}returnthis.front.value;}}module.exports=Queue;
Tests
"use strict";constStack=require("./Stack");constQueue=require("./Queue");letnewStack=newStack();// Can successfully push onto a stacknewStack.push("10");//Can successfully push multiple values onto a stacknewStack.push("20");newStack.push("30");newStack.push("40");//Can successfully pop off the stacknewStack.pop();//Can successfully empty a stack after multiple popsnewStack.pop();newStack.pop();newStack.pop();//Can successfully peek the next item on the stacknewStack.push("40");newStack.peek();//Can successfully instantiate an empty stacknewStack.isEmpty();//Calling pop or peek on empty stack raises exceptionnewStack.pop();newStack.pop();//__________________letnewQueue=newQueue();// Can successfully enqueue into a queuenewQueue.enqueue("10");// Can successfully enqueue multiple values into a queuenewQueue.enqueue("20");newQueue.enqueue("30");// Can successfully dequeue out of a queue the expected valuenewQueue.dequeue();// Can successfully peek into a queue, seeing the expected valuenewQueue.peek();// Can successfully empty a queue after multiple dequeuesnewQueue.dequeue();newQueue.dequeue();// Can successfully instantiate an empty queuenewQueue.isEmpty();// Calling dequeue or peek on empty queue raises exceptionnewQueue.dequeue();