Reputation: 425
Why should you implement a stack or queue when the same function can be achived with a list?
Upvotes: 0
Views: 357
Reputation: 15940
http://introcs.cs.princeton.edu/java/43stack/
Above link will explain you the clear picture why they have introduced
These new structures have introduced just keep faster performance in mind and Instead of rewriting the classes again...
Upvotes: 2
Reputation: 1202
Generally, when implementing a more specific data structure with more specific purpose, better efficiency can be achieved both in data structure implementation and in the usage API.
Upvotes: 2
Reputation: 4330
Why allow direct access to all elements in your collection when you only want/need to allow access to the front or tail element?
Additionally, stack.pop()
is easier to understand than list.remove(list.size()-1)
, and the same applies to most of the other stack and queue operations.
Upvotes: 4
Reputation: 137282
stack and queue may be based on list, but they have a specific behavior. It requires less implementation on your side, and gives you a more concrete and clarified behavior.
Upvotes: 6