19
loading...
This website collects cookies to deliver better user experience
// Stack Is nothing but a vector that follows LIFO order , it has // push , pop and peep as main Functionalities
// defining a class stack first off all
class Stack {
// array(vector) is used to implement the stack
constructor(){
this.items = [];
}
}
// now lets create a push function
push(element){
// push element
this.items.push(element);
}
pop(){
if(this.item.length == 0){
return (" UNDERFLOW");
}
return this.items.pop(element);
}
// peek return's the top most element but does not delete it
peek(){
return this.items[this.items.length - 1] ;
}
// printStack
printStack(){
var str = "";
for (var i = 0; i < this.items.length; i++)
str += this.items[i] + " ";
return str;
}
// adding element to the stack for purpose of test
var stack = new Stack();
stack.push(20);
stack.push(30);
stack.push(50);
stack.pop();
stack.peek();
console.log(stack.printStack());