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.
(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.)
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
posted
0
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 .
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
posted
0
You are correct, Hunter McMillen; that did appear to be the theme of the original post.