views:

136

answers:

5

Currently the only stack I know anything about is Vector, I normally use this in place of an array but I understand that there is other types of stacks and they all suit different jobs.

The project I am currently working on requires me to be inserting objects in a certain position inside a stack, not always the front of the stack and I am under the impression that a Vector may not be the best class for this job.

Could somebody please give me a brief description of the other types of stacks available to me with the Java language and their advantages and disadvantages? Are these names homogeneous? E.g. Are they only used in the Java language or are they used as general terms in Computer Science?

Thank you

A: 

You can implement Stack using a Vector. If you are going to speak in terms on Java, you can use LinkedList class to implement Stack on your own. (java.util.Stack inherits Vector, that is the reason you may want to write your own stack using LinkedList)

You can find an example here

Bragboy
What does that mean, "Vector is a collection whereas Stack is a data structure"? Did you know that `java.util.Stack` is a subclass of `java.util.Vector`?
Jesper
Yes, I know its a subclass
Bragboy
@Jesper, he may have capitalized for emphasis, but he means little-s stack. The _concept_ of a stack is a data structure; `java.util.Stack` is merely one implementation of that structure.
Lord Torgamus
So what? A vector (or list) is also a data structure.
Jesper
@Jesper, fair enough; when I read it, my brain automatically converted the meaning of one and not the other. I suppose that's not helpful for someone who doesn't already understand the topic. Removing my upvote.
Lord Torgamus
+1  A: 

I suggest taking a look at this tutorial on the Java Collections Framework, which refers to all of the main datatypes in the java.util package that you would use for things like this (Lists, Vectors, Maps, Trees, etc).

matt b
+3  A: 

You mention that you are inserting things into the middle of your 'Stack.' Then I would recommend using LinkedList.

If you have already found the position that you need to enter a new element, the time to insert it is O(1). For a vector it is O(n) because you need to copy the backing array.

LinkedList also supports stack-like operations like adding onto the end, or popping off the end of the stack.

Take a look at the javadocs for the data structure to see what it all offers.

To answer your last question. The LinkedList is a very common data structure throughout computer science. It is also very commonly used to implement stacks because the push and pop operations are constant time, O(1).

jjnguy
A linked list is a rather memory-inefficient data structure and often not the best implementation of a stack; array-based lists provide amortized O(1) push and pop as well.
Michael Borgwardt
@Michael, but inserting into the middle of any array-backed structure has poor (O(n)) performance no matter what.
jjnguy
@jinguy: true, but they offer O(1) random access. If you need both (which is usually the case) then these cancel each other out, and memory efficiency makes the array-based implementation the better choice. Only if you often need to add/remove during iteration and don't need random access is the linked list a good choice.
Michael Borgwardt
Inserts on LinkedList are O(1), but finding the nth element in the middle of the list is still O(n). If the use case is that inserts are much less frequent than searches, the resize penalty of ArrayList may be irrelevant compared to a large number of linear searches. If almost all the work is searches, a HashSet or LinkedHashSet may be even better.
AngerClown
+1  A: 

The API documentation of java.util.Stack advises you to use one of the implementations of interface java.util.Deque, for example java.util.ArrayDeque or java.util.LinkedList.

A LinkedList is efficient for inserting elements in the middle and removing an element from the head or tail (but it's inefficient for looking up a random element).

Jesper
+1  A: 
  • A stack is an abstract data type characterized by LIFO behaviour or push/pop operations
  • A list is an abstract data type characterized by having its elemets accessible through a numerical index.
  • An array is a low-level implementation of a list
  • java.util.List is the list type represented as a Java interface
  • java.util.Deque is a Java interface that provides both LIFO and FIFO queue behaviour (and thus stack behaviour as a subset)
  • java.util.Vector is an obsolete implementation of a list (based on an auto-resizing array) that should not be used anymore
  • java.util.ArrayList is its modernized replacement
  • java.util.Stack is an obsolete implementation of a stack that consists of adding some stack-like methods to a Vector, which is not a good way to do it.
  • java.util.ArrayDeque is a modern implementation of the Deque interface
  • java.util.LinkedList is a different implementation of a list (that also implements the Deque interface) that has a number of big disadvantages and should only be used in very specific cases (it is better when you need to insert or remove elemnts in the list very often).

Basically, if you want a stack, use the Deque interface and ArrayDeque as implementation class (except in the rare case where LinkedList is better). If you want a list, use ArrayList

Michael Borgwardt
@Michael, he specifies that he will be doing inserts into the middle of the 'stack' often.
jjnguy
@Michael, I don't think that a 'Stack' is the data structure that is actually needed. (Stacks, in the pure sense, only allow access to the element that was most recently added)
jjnguy
Note the Vector and Stack are obsolete because all operations are synchronized. The newer implementations are not synchronized and it's up to the user to synchronize as necessary or wrap the list with a synchronized wrapper class (see java.util.Collections.synchronizedList()).
AngerClown
@jinguy: "often" is not specified :) Nor is the absence of random access.
Michael Borgwardt