What happens when you compile and execute the code?
NormR1
Posting Sage
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16
The printBackward method should be written recursive in a singly list backward.
That's the keyword, recursive. :) Your implementation is not recursive but rather iterative. Do you know what recursive is?
Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 375
Skill Endorsements: 17
public void printBackward(int n)
...
addLast(list1); printBackward(); count --;
Despite the missing parameter that looks like an attempt at recursion to me :)
JamesCherrill
... trying to help
8,527 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30
am cramming for an exam tomorrow
Ooops! Probably better to spend your time on a few other topics because you could burn a lot of hours trying to get this one straight.
ps: Anyone else know a good answer to this one? I can't see any sensible way to solve it within all the contraints.
JamesCherrill
... trying to help
8,527 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30
I am sorry that your recursive call is actually creating an infinite loop. From what I see there, my first thought about printBackward() method is that it would return a string. But then I saw addLast(), I believe that you would need to reverse the list before you manipulate with the list. Below is what I think what should happen...
ListIterator<E> itr = /**Initialisation 1**/ list1.listIterator();
LinkedList<E> backwardList = /**Initialisation 2**/ new LinkedList<E>();
createBackwardList(itr, backwardList);
// now the backwardList is containing backward linked list data
private void createBackwardList(ListIterator<E> itr, LinkedList<E> list) {
if (itr.hasNext()) { // has next item
E item = itr.next(); // extract the item
printBackward(itr, list); // keep looking for the last item
list.addLast(item); // add back
}
}
Look at the method createBackwardList(), it is a simple recursive method accepting the original list iterator and an empty list at first.
First, it check whether the iterator still contains more item, if so, get the item from the iterator and keep calling until it finds nothing in the list. Once it find nothing, each of the item that is extracted from the list will be added back to the empty list one by one from the last one it visited. Once it is done, the original empty list will be filled with backward list. Hope this help you understanding recursive...
Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 375
Skill Endorsements: 17
"You should not introduce any new variable tinto the method. In your answer,", and it also imples no changes to the signature of the method, otherwize your solution is the (only) one I was considering
JamesCherrill
... trying to help
8,527 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30
Hmm... I don't know if what the code portion he gave was the original... I mean I would rather want to see the empty shell of the code to work with. :) But well, that's one way I would solve the problem. :)
Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 375
Skill Endorsements: 17
JamesCherrill
... trying to help
8,527 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30