How to Implement a Stack in Node.js

Petros Koulianos
Mar 31 · 3 min read

Stack is one of the most important data structures in programming, but how can we implement a stack in javascript? Let’s dig in.

Photo by Nathan Dumlao on Unsplash

1.Introduction

The stack is a fundamental abstract data structure for computer science that serves a collection of elements with the LIFO method and two main operations:

  • push (add a new element to stack)
  • pop (remove the top element)

Stacks are used mainly by algorithms(Backtracking,depth-first search,…), OS(operational systems), programming languages(memory management, call stack,… ), frameworks and many more applications.

2.Implementation

We’ll implement a stack with an array and we going to create a class with the following functions:

  • isEmpty (print if the stack is empty or not)
  • length (print the number of elements in the stack)
  • push (add a new element to stack)
  • pop (get and remove the top element)
  • printStack (print all elements in stack)

Create a new project with node.js

mkdir stackjs
npm init -y

Stack Class

Let’s build a Stack class first, create a file stack.js and paste the following code:

empty stack
class Stack {constructor() {//array used to store the stacks elementsthis.stack= [];    }}module.exports = Stack;

The Stack class has 1 property:

stack: is an array to store the stack elements

Push operation

push operation

The push operation adds one element to the top of the stack.

// push operationpush(element) {// add the element into the stackthis.stack.push(element);}

Pop operation

pop operation

The pop operation removes and returns the last element from the stack

// pop operationpop() {// return top most element from the stack// and removes it// Underflow if stack is emptyif (this.stack.length == 0)return "Stack Is Empty";return this.stack.pop();}

IsEmpty operation

IsEmpty operation checks if the stack length is empty or not

// isEmpty operationisEmpty() {// return true if stack is emptyreturn this.stack.length == 0;}

Length operation

Length operation returns the actual length of the stack

// length operationlength() {// return stack legthreturn this.stack.length;}

PrintStack operation

PrintStack operation iterates and prints all stack elements

// printStack functionprintStack() {var stringBuilder = "";for (var item = 0; item < this.stack.length; item++)stringBuilder += `${item}:` + this.stack[item] + '\n';return stringBuilder;}

The final class with all the functions.

stack.js

3.Test the Stack Class

Now we will try to test the stack we have built.

Create a new file index.js

index.js

Run index.js

node index.js
//console output
**Check if the stack is empty
true
**Check if the stack has zero length
0
**Pop from the empty stack
Underflow
**Push 3 memory slots to the stack
stack length:3
0:0x0000
1:0x0001
2:0x0002
**Pop the last memory slot from the stack
0x0002
stack length:2
0:0x0000
1:0x0001

4.Conclusion

Learning and using data structures can transform you into a great software developer.

References

Thanks for reading my story

Feel free to comment me for any ideas, changes, etc.

A note from JavaScript In Plain English: We are always interested in helping to promote quality content. If you have an article that you would like to submit to JavaScript In Plain English, send us an email at submissions@javascriptinplainenglish.com with your Medium username and we will get you added as a writer.

JavaScript In Plain English

New articles every day.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store