Suggestions for LinkedList Subset method
Hello folks,
I am looking for suggestions on creating a LinkedList subset method. Any guidance in terms of pseudocode should help me on my merry way. Here is what is being asked, any suggestions would be appreciated!
public LinkedList subset (int a, int b);
This method receives two integer parameters a and b and returns a LinkedList which is a subset of this LinkedList between the indexes a and b. a must be greater than zero and less than b and b must be less than the length of the list. i.e.. the subset of (1, 2, 7, 4, 5) from 2 to 4 is (2, 7, 4).
[583 byte] By [
cases] at [2007-11-20 11:46:26]

# 1 Re: Suggestions for LinkedList Subset method
Rather than us telling you how to do it why don't you tell us how you think you can solve the problem and we'll offer you guidance, that way you'll learn a lot more. If you can't write the code then show us psuedo code and if you can't do that just write out a description of how it might be solved.
keang at 2007-11-10 2:14:02 >

# 2 Re: Suggestions for LinkedList Subset method
ok since we are working with two ints I guess the pseudocode would look something like this:
This method receives two integer parameters a and b and returns a LinkedList which is a subset of this LinkedList between the indexes a and b. a must be greater than zero and less than b and b must be less than the length of the list. i.e.. the subset of (1, 2, 7, 4, 5) from 2 to 4 is (2, 7, 4).
while (a || b > 0 && a < b)
{
if a = number and b = number
print range in between minus a and b
}
There is obviously more to this such as the fact that b must less then the length of the list. If we are only supplying two integers to this method how does the method know what numbers to fill in between the first int and the last int?
cases at 2007-11-10 2:15:02 >

# 3 Re: Suggestions for LinkedList Subset method
ok forget the code, pseudo code and computers and think about how you personally would solve this problem. Lets say you had a list of 10 items and I asked you to give me list of the third to seventh items, how would you do it?
keang at 2007-11-10 2:16:06 >

# 4 Re: Suggestions for LinkedList Subset method
Right now you're speaking my language. Very basic and straight to the point. I guess this is the only way I'll understand how this works.
Ok if you gave me a list of 1 to 10 I would minus 2 from the beginning of the set and minus two from the end of the set to give us 3 to 7. How would this translate into pseudocode though?
cases at 2007-11-10 2:17:00 >

# 5 Re: Suggestions for LinkedList Subset method
...if you gave me a list of 1 to 10 I would minus 2 from the beginning of the set and minus two from the end of the set to give us 3 to 7.I think the point is that you should forget about code and pseudocode, and concentrate on the algorithm - the sequence of steps needed to achieve the objective. You just keep restating the original requirement in different ways.
If someone gives you a linked list of 10 items (suppose it's a set of beads), and asks you to give back items 3 to 7 as a smaller set of beads, what are the steps necessary to achieve that? (you say you would minus two from the beginning and two from the end, but that would leave you with 6 items, 3 to 8 :rolleyes: )
First of all, what is the exact requirement - are you supposed to leave the original list intact and return a copy of the sublist, or can you destroy the original to extract the sublist?
Staying with the beads, how do you know which end of the beads is the start and which is the finish? (a computer must be told). When you say you would 'minus 2 from the beginning' what does that mean, exactly? Taken literally, it's meaningless - minus 2 from the beginning puts you at two beads before the start...
You might think about finding the start, counting forward two items, then... what? How do you remove the beads from the thread - without all the others slipping off? Now translate that to how a linked list is put together - if you split the original at that point, you have to make a new start point for your sublist - how is that done? Similarly, to remove the last 3 items - do you count backwards from the end, or forwards from the start of the sublist to find the split point? How do you make the split? How do you create the new end point?
If you are supposed to copy the sublist and leave the original intact, how do you copy the original items and link the copies together?
When you have exact and detailed answers to all those questions, writing the code to do it should be easy.
Computers are good at following instructions, but not at reading your mind...
D. Knuth
dlorde at 2007-11-10 2:18:05 >

# 6 Re: Suggestions for LinkedList Subset method
If someone gives you a linked list of 10 items (suppose it's a set of beads)Nice analogy
keang at 2007-11-10 2:19:06 >
