Javatpoint Logo
Javatpoint Logo

Implementing JavaScript Stack Using Array

In this section, we will learn about the implementation of Stack using JavaScript arrays.

What is a Stack

A data structure in which we place the elements in Last In First Out, i.e., LIFO principle. The LIFO principle means the arrangement of the element in such a manner that the element which is added recently gets removed first, and the element which was added initially gets removed in the last. One can understand the arrangement of the elements in Stack as arranging dishing plates where the plate which was kept first will be used in the last. Such an arrangement is known as LIFO.

The Stack consists of two main functions, which are push() and pop(). Both these operations of Stack occur at the top of the Stack. The push() operation is for inserting/adding elements to the Stack, and the pop() function, on the other hand, is used for removing/popping an element from the Stack. The push() and pop() operations occur at the top because, in a stack, the element is always pushed and popped out from the top only.

Operations on Stack

There are the following operations which are performed on a stack:

  • push(): The push() operation is used for adding elements to the Stack.
  • pop(): The pop() operation is used for removing elements from the Stack.
  • peek(): The peek() operation is used for getting the top element present in the Stack.
  • length(): The length() operation is used for returning the length of the Stack.
  • search(): The search() operation is used for searching elements whether present in the Stack.
  • print(): The print() operation is used for printing elements of the Stack.
  • isEmpty(): The isEmpty() operation is to check if the stack is empty.

Now, we will discuss the implementation of Stack and its methods (discussed above).

Implementing Stack and its operations

In order to implement stack data structure, we need to create a stack class as shown below:

In the above code:

  1. We have created a class named stck.
  2. Under it, a constructor is created in which we have used two attributes i.e., ele and top. The ele is the array element that will add elements in the Stack, and as we know that in a stack, elements are added from the top of the Stack. So, we have created a top variable which points to the index of the element which is at the top.
  3. Both attributes are fetched via this The 'this' keyword is used to get the current value.

The push() operation

A stack method for adding elements to the top position.

An example to understand the use of push() method is shown below:

In the above code:

  1. We have created a function stackpush() in which we have passed an argument as e. The argument e will contain the value that will be inserted in the Stack.
  2. Under the function, using this we have accessed the value of e to the ele array and to the top.
  3. Now, the value of top is increased by 1 because the top variable has to point to the next empty array index in the Stack.

The pop() operation

The pop() method of Stack is used for removing/deleting elements from the top position of the Stack.

An example of stackpop() operation is shown below:

In the above code:

  1. We have created a stackpop () function under which the first step is to decrease the value of top by 1. It is because the top variable needs to point to the position of the previous element.
  2. In the next step, the value which is at the top of the Stack will be popped out using this operator.

The length () operation

The length () operation of the Stack is used to return the length of the Stack using the top variable.

Below is an example of using length () operation:

In the above code, we have created a stacklength () function, and it will return the length by calculating from the top of the Stack.

The peek () operation

A stack operation for fetching/getting the value that is present at the top of the Stack.

Below is an example to understand the practical implementation of the peek () function:

  1. In the above code, the peek () function returns the element present at the top of the Stack.
  2. We have used top - 1 as the top variable points to the top position in the Stack where the element is added.

The print () operation

The print () operation is used for printing the elements present in the Stack. Thus, it is like the printf in C programming.

Below is an example that shows the implementation of print () operation:

In the above code:

  1. We have created a function print () where we have initialized a variable t with top - 1 value.
  2. Next, a while loop is used in order to print all the values of the Stack from the top.
  3. The loop will begin from the last to the top, i.e., upto the 0th
  4. The value on each array index will be printed as per the index value.
  5. Lastly, the value is decremented as t--.

The reverse () operation

The reverse () operation of Stack is used for reversing the order of Stack so that the values of the Stack get printed in reverse order.

Below is an example that explains the implementation of the reverse () function:

In the above code:

  1. We have created a function reverse () using recursion.
  2. After this, another function rev () is created that has index as its parameter.
  3. Under the rev () function, we have used the if statement in which if the index value is not equal to, the reverse stack elements will be calculated.
  4. Finally, the reversed stack elements will get printed.
  5. These are some of the stack methods which we have practically implemented.

Combined Code Implementation

Let's see a full implementation of the stack with different operations. The implemented example is shown below:

You can implement the above code and understand it practically.

Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA