Switch position of nodes on list
I have a Generic Linked list in Java.
Code :
class Node{
T info;
Node next;
Node (T x){
info = x;
next = null;
}
}
I want to switch positions of two nodes on a list. Where a was I want to put b and where b was I want to put a.
Code :
public void switcherT a, T b){
// Stuck.
}
Please help.
Re: Switch position of nodes on list
To figure out the logic for this kind of problem I take piece of paper and draw a linked list with several nodes on it. Then work out the logic for removing a node, remember where it was, remove the second node, put the first node in the list where the second node was and then putting the second node into the list where the first node was.
Re: Switch position of nodes on list
Thanks for mocking me on both my threads...
Re: Switch position of nodes on list
You are mistaken. What I suggested is the way that I would solve the problem. Before you write any code you must understand what the code is supposed to do. Drawing diagrams on paper of the before and after condition and then working out how to do the steps to get from the before to the after condition is how you get to understand the problem. You must do those steps.
If you have already done that step and are now ready to write the code, can you explain which step in the list of steps the program must do that you are having problems with?
Re: Switch position of nodes on list
Quote:
Originally Posted by
Neobs
Thanks for mocking me on both my threads...
Nobody is mocking you, despite your rude tone directed towards somebody who is trying to help you, for free, in his own spare time.
He's trying to help you think through the problem, which is exactly what you should be doing. This is a technical forum for technical questions, not general "please help" type questions. He could have simply told you to google it or look it up on wikipedia, but instead he's trying to help you step through the logic of your problem. I highly suggest you pay more attention and give more respect and appreciation to him.
I'm much less tolerant with your kind of rudeness than Norm has been so far, so if you don't adjust your attitude, I'm going to ban you.