| by Arround The Web | No comments

Implementation of Stack in JavaScript

Stacks are linear data structures which follow the principle of LIFO. LIFO stands for last in first out meaning that the most recently added item is the first one to be removed. This data structure is named stack as an analogy to real world stacks e.g., a stack of cookies in a cookie jar or a stack of books on a bookshelf. In stack insertion and extraction can only be done on one end i.e., the top of the stack. For example, if we want to eat a cookie, we will get the top one first and then the 2nd and so forth.

This post will be all about the implementation of stack in JavaScript. As we are working with JavaScript we won’t be worried about the size of the stack as the size of the JavaScript objects can grow dynamically.

Implementation of Stack in JavaScript

We will use a JavaScript class to implement the stack data structure. The stack class will contain an array in its constructor which will be used to store elements in the stack. The class will also define different methods which will be used to manipulate the data stored inside the stack. The most basic methods of the array are the insert() and extract() methods which are used to add and remove elements from the top of the stack.

The stack class also defines other methods such as peek(), isEmpty(), clear(), print() and size() as well:

 class stack {
  constructor() {

    this.elements = [];

  }

  //Places an item on top of the stack

  insert(element) {

    this.elements.push(element);

  }

  //Removes an item from the top of the stack

  extract() {

    this.elements.pop();

  }

  //Returns the top most element of the stack

  peek() {

    return this.elements[this.elements.length - 1];

  }
  //Checks if stack is empty

  isEmpty() {

    return this.elements.length == 0;

  }

  //Prints the whole stack

  print() {
    for (let i = 0; i < this.elements.length; i++) {
      console.log(this.elements[i]);
    }
   
  }
  //Returns the size of the stack

  size() {

    return this.elements.length;

  }

  //clears the stack

  clear() {
    this.elements = [];
  }

}

 

Pushing and Popping elements from the stack

The most basic operation of the stack is to insert and extract elements from the top of the stack. The stack class provides two methods for these operations:


The first line of the above-mentioned code declares a new stack named s. Then the insert() method is used to insert four elements to the stack, two of which are then removed by the extract() method.

How to get the top element from the stack

The stack class defines the peek() method to get the top element from the stack:


How to check if the stack is empty?

The class also defines a method which can be used to check if the stack is empty:


How to print the whole stack?

The print() method can be called to print the whole stack


How to check the size of the stack?

The size() method uses the .length property to get the size of the stack:


How to clear the whole stack?

Simply invoke the clear() method to remove every element of the stack:

Conclusion

Stacks are useful data structures with many real-world applications such as browser history, undo button in text editors and call logs. All of these applications follow the LIFO principle e.g., the back button in the browser takes back to the last visited page and the first entry of the call log is always the latest call.

The implementation of stack in JavaScript is really easy as it has the inbuilt push and pop methods for arrays. This article demonstrates the process of implementation of stack in JavaScript.

Share Button

Source: linuxhint.com

Leave a Reply