199user911
199user911

Reputation: 425

Why implement stacks and queues java

Why should you implement a stack or queue when the same function can be achived with a list?

Upvotes: 0

Views: 357

Answers (4)

gmhk
gmhk

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

amotzg
amotzg

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

arne.b
arne.b

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

MByD
MByD

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

Related Questions