trosy
trosy

Reputation: 93

Should stacks be made from arrays or linked list java?

I have to write a stack for class and while I understand the concept of how a stack works, I wasn't told if they are made using an array or a linked list or something else? How are most stacks created?

Upvotes: 1

Views: 107

Answers (3)

Mohammad Najar
Mohammad Najar

Reputation: 2009

ArrayDeque is a solid class implementation of the stack concept. This class has implemented stack in the most efficient way. Please look at the class implementation for the details of various methods.

http://www.docjar.com/html/api/java/util/ArrayDeque.java.html

More specifically, look at public E pollFirst(){...} and public void addFirst(E e)

Upvotes: 4

Durandal
Durandal

Reputation: 20059

Both options, array and linked list are appropiate.

A linked list may be simpler because you needn't worry about the array size. An array based implementation on the other hand may have better runtime behavior and can be easier to debug (because its easier to view the array than a linked list in the debugger).

Chose whatever you're comfortable with.

Upvotes: 1

ControlAltDel
ControlAltDel

Reputation: 35011

java.util.Stack is a subclass of java.util.Vector which was a Thread-safe precursor to ArrayList. Hope that helps.

Upvotes: 1

Related Questions