File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes efficient Queue vs Stack Implementation Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "efficient Queue vs Stack Implementation " Watch "efficient Queue vs Stack Implementation " New topic
Author

efficient Queue vs Stack Implementation

Steven Rodeo
Ranch Hand

Joined: Mar 06, 2008
Posts: 72

Hello,

I've been thinking of implementing a Queue and a Stack data structure using either an Array / LinkedList (single /Double). Any one has any thoughts on which one might be a better choice. A reasoning would be great too.

Thanks a bunch!
_SM
Leandro Coutinho
Ranch Hand

Joined: Mar 04, 2009
Posts: 415
a = ArrayList
l = LinkedList

random access: a
reverse the list: l
inserting and deleting: l


more info: http://java.sun.com/developer/JDCTechTips/2002/tt0910.html
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

(Don't forget there are already implementations of both a stack (java.util.Stack, I think it's not recommended for general use) and queues (several, off of AbstractQueue), if you care.)
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 490

Array - Pro: You can access the whole stack or queue whenever you want.
Con: You can only have a fixed size. The size you originally declare it with.

LinkedList - Pro: you can have unlimited size, at least until your memory runs out.
Con: You have to search through the entire list to find something, because each part of the list (node) only knows where the next node is; it can't see the entire list.



Both have good uses. Which one you choose depends on what you need your program to do.

-Hunter


"If the facts don't fit the theory, get new facts" --Albert Einstein
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32708
    
    4
Nothing wrong with the existing Stack for general use, apart from the design problem that it extends Vector.

The fastest Stack and Queue implementation is probably ArrayDeque .
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

The Stack class says that a Deque impl should be used in preference to Stack; I just go by that since it's in the javadocs.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32708
    
    4
I had never noticed that it says to use a Deque instead of java.util.Stack. As I said, ArrayDeque says it is the fastest stack implementation.
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 490

If you are implementing them for the first time, I think doing it without the built in methods is a really good idea. Gives you a sense of what is really going on.

-Hunter
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32708
    
    4
You are correct, Hunter McMillen; that did appear to be the theme of the original post.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: efficient Queue vs Stack Implementation
 
Similar Threads
Interesting recursion
Stack to Queue
advantage stack over linked list or queue
Queue in Java
Stack and Queue Implementation