Computer Science Canada Java Data Structure |
Author: | Christ1m [ Tue Mar 17, 2009 6:34 pm ] |
Post subject: | Java Data Structure |
I need to write a recursive function that takes a linked list and an integer n, and returns a copy of the list, without the n-th element. The program should build a list containing the integers 0 to 9, then prompt the user for an input n, delete the n-th element, and print the result. So the user should see this behavior: number of the element to delete: 5 result: 0 1 2 3 4 6 7 8 9 I need some ideas on how the structure of the program should look like. |
Author: | Tony [ Tue Mar 17, 2009 6:42 pm ] |
Post subject: | Re: Java Data Structure |
Christ1m @ Tue Mar 17, 2009 6:34 pm wrote: I need some ideas on how the structure of the program should look like.
Christ1m @ Tue Mar 17, 2009 6:34 pm wrote: write a recursive function...
Your program should be a class with a recursive method. |
Author: | chili5 [ Tue Mar 17, 2009 6:44 pm ] |
Post subject: | RE:Java Data Structure |
That doesn't seem difficult. Do you know about the ArrayList or LinkedList? Or are you implementing your own data structure? Either way, you shouldn't have to use a recursive function to remove the n-th element. Just use the remove method? |
Author: | Tony [ Tue Mar 17, 2009 6:52 pm ] |
Post subject: | Re: RE:Java Data Structure |
chili5 @ Tue Mar 17, 2009 6:44 pm wrote: Either way, you shouldn't have to use a recursive function to remove the n-th element.
That would defeat the purpose of the exercise; if it explicitly asks for a recursive solution. Besides, recursion is your friend ![]() |
Author: | Christ1m [ Tue Mar 17, 2009 11:03 pm ] | ||||
Post subject: | Re: Java Data Structure | ||||
Right now im clueless. I dont know how to start the recursive function and the method. (Some basic pseudo code maybe helpful). Even though I dont have those functions, I wonder if Im heading to the right direction with the main method. Mod Edit: Remember to use syntax tags! Thanks ![]()
|
Author: | Tony [ Tue Mar 17, 2009 11:23 pm ] |
Post subject: | RE:Java Data Structure |
you "start" a recursive function from the end -- what is the base case? (that is, what is the most simple case, to which a solution is trivial? it likely has to do with the least number of elements). The rest of the recursive function is a step that brings current case closer to that base case (even if just by a bit). |
Author: | jbking [ Wed Mar 18, 2009 7:11 am ] |
Post subject: | Re: Java Data Structure |
Christ1m @ Tue Mar 17, 2009 4:34 pm wrote: I need to write a recursive function that takes a linked list and an integer n, and returns a copy of the list, without the n-th element.
The program should build a list containing the integers 0 to 9, then prompt the user for an input n, delete the n-th element, and print the result. So the user should see this behavior: number of the element to delete: 5 result: 0 1 2 3 4 6 7 8 9 I need some ideas on how the structure of the program should look like. Are you sure about that example? I ask because if n is 1 then you are removing the first element which is 0 and not 1. This is being a bit nit picky but I think it is a fair question as the list isn't that the n-th element has value n but n-1, creating to my mind some confusion which I'd hope some other people may have in reading the question. |
Author: | Christ1m [ Wed Mar 18, 2009 1:52 pm ] | ||
Post subject: | Re: Java Data Structure | ||
Im having problems creating the recursive functions. I need some ideas. |