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
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.