Implementation of Stack in JavaScript


In this article, we would be implementing Stack Data Structure in Javascript. Stack is a very useful data structure and has a wide range of application. Stack is a linear data structure in which addition or removal of 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.

Lets see an example of an stack class using array in Java script:-
Examples:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Stack class
class Stack {
  
    // Array is used to implement stack
    constructor()
    {
        this.items = [];
    }
  
    // Functions to be implemented
    // push(item)
    // pop()
    // peek()
    // isEmpty()
    // printStack()
}

chevron_right


As you can see 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.

Now let’s see implementation of each method:

  1. Push: Adds an element to the stack
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // push function
    push(element)
    {
        // push element into the items
        this.items.push(element);
    }

    chevron_right

    
    

    This method adds an element at the top of the stack.

  2. Pop() : Removes an element from the stack, if the function is call on an empty stack it indicates “Underflow”

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // pop function
    pop()
    {
        // return top most element in the stack
        // and removes it from the stack
        // Underflow if stack is empty
        if (this.items.length == 0)
            return "Underflow";
        return this.items.pop();
    }

    chevron_right

    
    

    This method returns the topmost element of stack and removes it. Return underflow when called on an empty stack.

  3. Peek() : returns the top most elements in the stack, but doesn’t delete it.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // peek function
    peek()
    {
        // return the top most element from the stack
        // but does'nt delete it.
        return this.items[this.items.length - 1];
    }

    chevron_right

    
    

    Return the topmost element without removing it from the stack.

Helper methods

This are the three basic operation perform by an Stack lets declare some helper method which can be useful while working with stack.

  1. isEmpty() : return true if the stack is empty
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // isEmpty function
    isEmpty()
    {
        // return true if stack is empty
        return this.items.length == 0;
    }

    chevron_right

    
    

    Returns true if the stack is empty.

  2. printStack() : This method returns a string in which all the element of an stack is concatenated.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // printStack function
    printStack()
    {
        var str = "";
        for (var i = 0; i < this.items.length; i++)
            str += this.items[i] + " ";
        return str;
    }

    chevron_right

    
    

    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

In this example we would create an object of stack class and test few functions of it.

filter_none

edit
close

play_arrow

link
brightness_4
code

// creating object for stack class
var stack = new Stack();
  
// testing isEmpty and pop on an empty stack
  
// returns false
console.log(stack.isEmpty()); 
  
// returns Underflow
console.log(stack.pop()); 

chevron_right


Some more functions of stack class
Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Adding element to the stack
stack.push(10);
stack.push(20);
stack.push(30);
  
// Printing the stack element
// prints [10, 20, 30]
console.log(stack.printStack());
  
// returns 30
console.log(stack.peek());
  
// returns 30 and remove it from stack
console.log(stack.pop());
  
// returns [10, 20]
console.log(stack.printStack()); 

chevron_right


Once we are done with implementing and testing the stack class now we can use it in different application.

Application: Evaluation of Postfix Expression

In this example, we would use the above stack class to evaluate postfix expression

filter_none

edit
close

play_arrow

link
brightness_4
code

// Performs Postfix Evaluation on a given exp
function postFixEvaluation(exp)
{
    var stack = new Stack();
    for (var i = 0; i < exp.length; i++) {
        var c = exp[i];
        if (!isNaN(c))
            stack.push(c - '0');
        else {
            var val1 = stack.pop();
            var val2 = stack.pop();
            if (val1 == "Underflow" || val2 == "Underflow")
                return "Can't 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();
}
  
// calling the above method
// returns 9
console.log(postFixEvaluation("235*+8-")); 
  
// returns postfix evaluation can't be performed
console.log(postFixEvaluation("23*+"));

chevron_right


This article is contributed by Sumit Ghosh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up


Article Tags :

4


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.