1

not sure if this is a valid question though, but here it goes. I've had this doubts when it comes to different lists(linkedList, queues, stacks, etc.) and recursion algorithms. I just don't really understand when should I use them or why. I know how to implement them, but I'm not really sure why should I use a list instead of a regular array or why should a recursion be done instead of a for. I'm about to graduate in 1 1/2 years and I don't wanna go look for a job without knowing this.

Thanks in advance and if you could give me an example of a problem where I should use any of them, I would greatly appreciate it

Jcorretjer
  • 359
  • 1
  • 5
  • 20
  • Recursive methods could be more simple to code that iterative. For example, navigating through the tree or graph nodes. For using List agains array on recursive methods, that really depends on the programmer, but usually is faster to code over Lists than array based, especially in cases when you don't know how much data you can hold. – Luiggi Mendoza Oct 02 '12 at 03:13
  • I am about to ask a BUNCH of questions, I hope it's not too much of a drag ^^' – Jcorretjer Oct 02 '12 at 03:17
  • [This thread](http://stackoverflow.com/questions/3021/what-is-recursion-and-when-should-i-use-it) offers different views on recursion. With respect to different data structures, a language like Java organizes List and Queue as interfaces and LinkedList & Stack as implementations of those interfaces. [Read Java's definition of LinkedList](http://docs.oracle.com/javase/6/docs/api/java/util/LinkedList.html) to see how things are organized and what they offer, by definition. – Kale McNaney Oct 02 '12 at 03:18
  • @LuiggiMendoza, thanks. Your answer helped me understand one of the "whys" lol – Jcorretjer Oct 02 '12 at 03:49

3 Answers3

0

Take a look at this article that compares Recursion with Iteration : http://www.codeproject.com/Articles/21194/Iterative-vs-Recursive-Approaches
Go thru Array or List in Java. Which is faster? for a better understanding of lists and arrays.

Community
  • 1
  • 1
venkatKA
  • 2,379
  • 1
  • 16
  • 22
  • so recursion is easier to write, but runs slower and iteration is the other way around. GOT IT! I got why should I use lists instead of an array, but I still have one more question related to lists and it's use. And that is, why should I use a stack instead of a queue or a linkedList? What is the advantage of using one data structure over another one? (not sure if know what I mean) – Jcorretjer Oct 02 '12 at 03:32
  • @chelo666 The data structure that you employ solely depends on the purpose or the application. For instance, you can switch between postfix and infix expressions using stack rather than a queue. But Scheduling can be done using Queue. So, think about it. I guess you understand it enough to give me a thumbs up ;) – venkatKA Oct 02 '12 at 03:36
0

Here's a page that describes the different Java Collections. Basically it will depend on your content and how you want to use them. For example, when I need a collection of values with No duplicates I use a Set. When duplicates do not matter I use a List because they perform fast and are easier to code in my opinion. There are also some things like Queues where they store data in the order you enter them.

Collections Description

Logan
  • 2,299
  • 17
  • 20
0

Recursion and iteration are equivalent in terms of "what they can do". The reason you would use recursion instead of iteration is that it simplifies how to write certain algorithms. For example, traversing a binary tree recursively produces clearer code than trying to do it iteratively; writing quicksort recursively is easier and cleaner than doing it iteratively. Recursion is simply the act of utilizing the program stack to store state; you can convert any recursive algorithm to its iterative counterpart by storing the stack yourself. I'd suggest you work with a language like Racket for a while. It will get this into your head far better than my words can.

You would generally use a list instead of an array where frequent deletions were needed, and you don't need to access specific elements often. Deletion from an array is an O(n) operation as everything after the deleted element needs to be shifted one index to the left. Queues are not necessarily list structures, they can be represented utilizing arrays. Queues and Stacks are useful in many algorithms, such as breadth and depth-first graph searches. I'd suggest you grab a data structures and algorithms book for this, having the algorithms illustrate why certain data structures are useful will be invaluable.

Yuushi
  • 22,789
  • 6
  • 58
  • 73