Implementation of Stack in JavaScript
Last Updated :
20 Feb, 2024
In this article, we will be implementing Stack Data Structure in JavaScript. Stack is a very useful data structure and has a wide range of applications. Stack is a linear data structure in which the addition or removal of an element follows a particular order i.e. LIFO(Last in First Out) AND FILO(First in Last Out).
Note : Assuming the stack can grow dynamically we are not considering the overflow condition.
Examples: Below is an example of a stack class using an array in JavaScript.
Javascript
class Stack {
constructor()
{
this .items = [];
}
}
|
As you can see in the above definition we have created a skeleton of a stack class which contains a constructor in which we declare an array to implement stack. Hence, with the creation of an object of a stack class, this constructor would be called automatically.
Methods in Stack
- Push: Used to add an element to the stack. This method adds an element at the top of the stack.
Javascript
push(element)
{
this .items.push(element);
}
|
- Pop() : Removes an element from the stack, if the function is call on an empty stack it indicates “Underflow”. This method returns the topmost element of stack and removes it. Return underflow when called on an empty stack.
Javascript
pop()
{
if ( this .items.length == 0)
return "Underflow" ;
return this .items.pop();
}
|
- Peek() : returns the top most elements in the stack, but doesn’t delete it. It Return the topmost element without removing it from the stack.
Javascript
peek()
{
return this .items[ this .items.length - 1];
}
|
Helper methods
- isEmpty() : return true if the stack is empty. Returns true if the stack is empty.
Javascript
isEmpty()
{
return this .items.length == 0;
}
|
- printStack() : This method returns a string in which all the element of an stack is concatenated.
Javascript
printStack()
{
let str = "" ;
for (let i = 0; i < this .items.length; i++)
str += this .items[i] + " " ;
return str;
}
|
Note : Different helper function can be declared in Stack class as per the requirement. Now as we are done with defining the stack class lets use it.
Sample Functions
Example: In this example we would create an object of stack class and test few functions of it.
Javascript
let stack = new Stack();
console.log(stack.isEmpty());
console.log(stack.pop());
|
Example: Some more functions of stack class
Javascript
stack.push(10);
stack.push(20);
stack.push(30);
console.log(stack.printStack());
console.log(stack.peek());
console.log(stack.pop());
console.log(stack.printStack());
|
Example: In this example, we would use the above stack class to evaluate postfix expression.
Javascript
function postFixEvaluation(exp)
{
let stack = new Stack();
for (let i = 0; i < exp.length; i++) {
let c = exp[i];
if (!isNaN(c))
stack.push(c - '0' );
else {
let val1 = stack.pop();
let val2 = stack.pop();
if (val1 == "Underflow" || val2 == "Underflow" )
return "Can not perform postfix evaluation" ;
switch (c) {
case '+' :
stack.push(val2 + val1);
break ;
case '-' :
stack.push(val2 - val1);
break ;
case '/' :
stack.push(val2 / val1);
break ;
case '*' :
stack.push(val2 * val1);
break ;
}
}
}
return stack.pop();
}
console.log(postFixEvaluation( "235*+8-" ));
console.log(postFixEvaluation( "23*+" ));
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...