Benefits of Stacks/Queues over ArrayList

In the title I’ve mentioned ArrayList, which is java specific. This is a general question that isn’t language-agnostic(A java ArrayList = JavaScript Array or a Python List, or a C++ vector), so what are the benefits of using stacks/queues. Why use stacks/queues(unless it’s a language like C where there is no built in feature of such) over a resizeable and easy to use collection? In a certain course I’m taking, despite the fact that Java has arraylists built in, they’re explaining how to implement a stack/queue which seems plain limiting. A FIFO or LIFO only principle doesn’t seem all that effective.

I would guess that this is a data structures course. If that is the case, then the entire point is to understand how data structures work at a low level.

Some times you don’t need a large, complex data structure, and it is handy to understand different types of basic structures to accomplish your task at hand.

Have you tried Googling to find applications of stacks and queues?
https://en.wikipedia.org/wiki/Stack_(abstract_data_type)#Applications_of_stacks
https://en.wikipedia.org/wiki/FIFO_(computing_and_electronics)

You can use these concepts to better understand how more complex data structures work. Also, stacks and queues are pretty important for operating systems, compilers, etc.

1 Like

The question, and the point of the class boils down to the following:

“Why use data-structure X over data-structure Y?”

Usually the answer comes down to knowing the pros and cons of using each. Its true ArrayList provides dynamic array allocation which makes it easier to use. Ease of use is a pro on the programmer side, but there are cons to such a data structure in practice.

Its also possible you wont need/want dynamic array allocation, making the pro unnecessary.
What the cons are I’ll leave to the reader, as I don’t want to give away the answer and remove the ability for you to find it yourself.

In most data structure focused courses, they have you implement specific data structures as a way to “understand how things work under the hood”. In the real world you will rarely implement your own data structures, and will use pre-made ones yourself, which are optimized for the task at hand.

However, just because you have all these fancy tools available, doesn’t mean you can go with the “easiest to use”. Its your job as a developer to pick the right tool for the job so your code works as intended and most importantly scales correctly, which again goes back to the goal of learning data structures and algorithms.

1 Like

Let me guess the flaws of using an ArrayList, ig an ArrayList is probably implemented via resizeable arrays, which are relatively expensive to use, as you gotta keep on copying over the previous array into a new array with more size and free the memory of the old array, and that slows down the actual process ig.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.