Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
November 21, 2020 04:54 pm GMT

How to create a Stack using JavaScript

JavaScript comes with some out of the box data structures. This includes Arrays and objects. Linked List, graphs, trees, queues, and stacks are not included with JavaScript. these data structures need to be constructed using a class. The data structures mention are important to know since different data structures excel at storing and retrieving data more efficiently than others depending on the scenario. Today we will be covering how to make a Stack.

What is a Stack?

Alt Text

A stack is a data structure that follows a simple rule. Last-in, first-out, or FIFO. You can think of the stack as a pile of dishes. If you add a dish you must put it on top of the pile and if you want to remove the dish you need to remove it from the top of the pile.

Alt Text

A common use for this data structure is the call stack. Functions are stack on top of each other until the function returns and functions will start popping off the stack.

What does a Stack Contain and how to build it?

class Stack{    constructor(){ = [];    }}
Enter fullscreen mode Exit fullscreen mode

To get started building a stack we need to create a stack class and inside the constructor, the object will be initialized with an empty array( );


    push(value){;        return this;    }
Enter fullscreen mode Exit fullscreen mode

The first instance method that will be covered push. push takes in a value as a parameter. Push adds nodes to the end of the array. Finally, return the instance of the class.


    pop(){        return;    }
Enter fullscreen mode Exit fullscreen mode

The instance method pop() removes values from the end of the array. We will be using array building methods to remove a value from the end of the array. The pop() method will be used. Calling pop on the array will return the


    peek(){        return[ - 1]    }
Enter fullscreen mode Exit fullscreen mode

The instance method peeks () returns the last value of an array.


    empty(){        if( === 0){            return true;        } else {            return false;         }    }
Enter fullscreen mode Exit fullscreen mode

Finally, the empty() instance method just returns true if there are any values in the stack or false if the stack is empty.

function sumArray(arr, sum = 0){    if(arr.length === 0) return sum;    sum = arr.pop() + sum;    return sumArray(arr, sum)}sumArray([1,2,3,4])
Enter fullscreen mode Exit fullscreen mode

Alt Text

Stacks are very simple to build using array methods and are frequently used in recursion. Recursion is when a function calls itself. You will have to get familiar with the call stack and recursion to be able to traverse trees and graph data structures. I hope this gave you a bit of insight into what stacks are and what they are used for.

Original Link:

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To