DEV Community

Play Button Pause Button
Nick Karnik
Nick Karnik

Posted on • Updated on

Implementing the Stack Data Structure in Javascript

In this video, we are implementing the Stack Data Structure in Javascript. While JavaScript Arrays can be used as a stack, this video is meant for educational purposes as we will go into more complex data structures in the future.

A stack is a simple data structure which is a container of objects that are inserted and removed according to the last in and first out principle. Elements can be added and removed only from the top.

Link to YouTube Video
Link to the CodeSandbox

Stack Data Structure


If this video was helpful, ❤️ it and subscribe to my YouTube channel.

Top comments (5)

Collapse
 
lewiscowles1986 profile image
Lewis Cowles

JS comes with stack LIFO and queue FIFO built-in using arrays.

The benefit of using an array instead of linked list? Well it can serialize more trivially given a start pointer and size.

Perhaps something worth considering (although maybe not for JS)

Also JS doesn't have support for explicit pointers, so not sure if it's confusing for people to use that terminology when discussing JS implemented Stack.

Thoughts?

Collapse
 
theoutlander profile image
Nick Karnik

You are right about JS Arrays. I should have mentioned that in the video. I was going to use this as a basis for future videos as we go into linked lists and more interesting data structures.

I think the exercise of building these basic structures will get people familiarized with something they’re already using.

As for pointers, I’ve seen people use that term in JS quite a bit. However, they’re probably not thinking of it in the C++ sense. Am I right about that assumption?

Collapse
 
lewiscowles1986 profile image
Lewis Cowles

I was thinking even the terms pop, push and shift, unshift would come up was all. Personally I think queue, dequeue would be better names but we can't go breaking public API's based on one persons persnickety notions ;-)

It's great you want people to get familiarised, I think that is another benefit to arrays, and certainly your examples will translate better to Java, Python or C.

On pointers, Asm / C with a layer of "my language gives me this" :-).

In CPP I'm currently re-learning for newer language features (supposed to be updating but it turns out I was writing C with classes for years). Basically everything is meant to be a reference unless you're systems-level and need the control / PITA which is manual pointers. So you might as well if you're handling C++ use references and if you need pointers write C (for learners or people not controlling space-vehicles or operating on tiny embedded packages).

Collapse
 
rajatkantinandi profile image
Rajat Kanti Nandi

JS has built in push & pop methods on Array prototype & those are more than enough for any stack use case.

Collapse
 
theoutlander profile image
Nick Karnik

This is not meant as a replacement. However, there is value in understanding underlying structures.