DEV Community

Cover image for Stacks vs. Queues In JavaScript

Stacks vs. Queues In JavaScript

Emma Bostian ✨ on April 26, 2019

Queues and stacks are two common data structures leveraged on technical interviews. Due to the fact that they’re quite similar in structure, they...
Collapse
 
felipernb profile image
Felipe Ribeiro

Nice article!

The only issue with implementing stacks and queues on top of dynamically sized arrays is that you don't have a guarantee on the complexity of the insertion or removal of elements, as the array might need to do some copying and resizing underneath. It might still be an amortized constant time, while a linked list would actually always give you O(1).

But maybe linked lists could be the next topic on the Data Structures With JavaScript series? :)

Collapse
 
felipperegazio profile image
Felippe Regazio

Awesome post, really clear. Thanks. Just a little issue:

enqueue(item): Remove the top item from the stack

dequeue(): Add an item to the top of the stack
Enter fullscreen mode Exit fullscreen mode

In this part of your post the descriptions are swaped i guess (sorry if i missunderstood your explanation).

Collapse
 
emmabostian profile image
Emma Bostian ✨

That's what happens when you copy & paste :P

Collapse
 
geompse profile image
Geompse

It is interesting but not true : stacks and queues are native to Javascript under the Array class. While both your classes may have a more readable usage, they have a larger memory footprint and may be slower than the native one. You might be better off extending the Array prototype and creating aliases...

Collapse
 
diek profile image
diek

Well, it is not native as we should understand native.

She is sweetening the usage of queues and stacks, people creating sugar versions of native implementations it's not only an awesome training but necessary to the community to grow.

Collapse
 
geompse profile image
Geompse

I agree but when I read "JavaScript doesn’t provide a native stack data structure, so we have to build our own with an array and a closure or a class." I do not understand "Let's make vanilla Javascript easier for beginners and learn more about it.".

It was meant to be constructive, much love.

PS: The fact that in an other contexts (java...) stacks are "more native" is true aswell (and can also be detailled)

Collapse
 
konrud profile image
Konstantin Rouda

I think that the description is wrong here it should be swapped and be changed to queue as we are implementing queue here and not the stack.

enqueue(item): Remove the top item from the stack
dequeue(): Add an item to the top of the stack

that is it should be set like so (and it should be changed to queue, instead of the stack)

enqueue(item): Add an item to the top of the queue
dequeue(): Remove the top item from the queue

Collapse
 
emmabostian profile image
Emma Bostian ✨

Copy paste ftw

Collapse
 
mortoray profile image
edA‑qa mort‑ora‑y

I'm a bit uncertain of your characterization of the complexity of stacks. In the general sense, stacks, independent of a language implementation, don't have any particular complexity guarantees. In the context of JavaScript, I don't believe the language guarantees any kind of complexity bounds.

In other languages, like C++, a stack/vector provides push in amortized constant time, and subscript access in constant time.

Only if your stack is implemented as a linked list would you need O(N) random access.

Collapse
 
nestedsoftware profile image
Nested Software • Edited

I believe your comment is correct, though the wording may be a bit confusing for some people. I had to read it a couple of times before it made sense to me :)

You're right that the ecmascript specification doesn't seem to provide any specific guarantee. In practice, I think we can usually count on the fact that Array.prototype.push is O(1) amortized, since that's the big-O for adding an item to a hash map, which seems to be how arrays are implemented in javascript behind the scenes.

Collapse
 
mortoray profile image
edA‑qa mort‑ora‑y

If I were to be pedantic, and you know how I love to be when it comes to complexity, hash maps don't have O(1) amortized push time. They have O(1) average-input (possibly amortized) push time. :)

Thread Thread
 
nestedsoftware profile image
Nested Software • Edited

Sigh. Yes, I believe it’s both! 😅

Collapse
 
danielsan profile image
Daniel Santana

A slightly faster alternative for the queue would be this:

enqueue(item) { return queue.push(item); }
dequeue() { return queue.shift(); }
peek() { return queue[0]; } // no need to calculate the length of the array or any math ;)
Collapse
 
johnson_cor profile image
Corey Johnson

This is awesome! Super clean! I'm pretty much a beginner with JS but what are the benefits of using a closure implementation?

Collapse
 
emmabostian profile image
Emma Bostian ✨

In all honesty, I'm not sure about the benefits. Previously, JS didn't have "class" syntax, therefore closures were necessary. But when the class keyword was released with ES6, I believe, that provided a new way to write this type of data structure.

Collapse
 
johnson_cor profile image
Corey Johnson

Oh! That's makes a lot of sense. I remember trying to do something similar to this using prototypes and it was not fun.

Collapse
 
dilantha111 profile image
Dilantha

Nice article. Thanks for sharing. Considering @geompse 's suggestion, I also tried an alternative approach using es6 classes and Array class. Let me know what you think. dev.to/dilantha111/stacks-and-queu...

Collapse
 
nickfazzpdx profile image
Nicholas Fazzolari

Great post. I remember having to implement these data structures in C++ a few years back. I need to review that code again. No memory management/pointers in JS makes the high-level mechanism of the particular data structure easier to see. Very cool.

Collapse
 
starboysharma profile image
Pankaj Sharma

Hello, first of all thanks for sharing this amazing article. I am new in JS and I understand both concept of Stack and Queue. I am curious to know how to use this method in real world. For Example If I want to search data from an array (Book 3) like your book example which is best way to start search LIFO or FIFO.

Collapse
 
gypsydave5 profile image
David Wickes

Nice post Emma - particularly liked the implementation using a closure!

Collapse
 
moremooredesign profile image
Jeremy Moore

I was a little thrown by the return value of every method invocation, until I refreshed my memory on the use of get.

Collapse
 
jahidhasan profile image
Jahid Hasan

Thank you for taking the time to explain this.

Collapse
 
phlickey profile image
Phil

Awesome.