I have placed comments under each line of code, please correct where necessary...
public void ReverseList()
{
CustomDoubleLinkedListItem node = this.RootNode.NextNode;
// Get a reference to the current root nodes next node? what is this previously set to and how, constructor?
CustomDoubleLinkedListItem previousNode;
// declare a field named previous node
CustomDoubleLinkedListItem nextNode;
// declare a field named next node
while (node != null)
{
nextNode = node.PreviousNode;
// next node is set to reference the nodes previous node
previousNode = node.NextNode;
// previous node is set to reference the nodes next node
node.PreviousNode = previousNode;
// the nodes previous node is set to reference the previous node
node.NextNode = nextNode;
// the nodes next node is set to reference the next node
//Handle the old root? what is meant by old root?
if (previousNode == null)
{
previousNode = this.RootNode.NextNode;
// previous node is set to reference the current root nodes next node
this.RootNode.NextNode = null;
// current root nodes next node is set to reference null
this.RootNode.PreviousNode = previousNode;
// set the root nodes previous node to reference the previous node
this.RootNode = node;
// set the current root node to reference the node, which node? old or current?
previousNode = null;
}
if (previousNode == this.RootNode)
{
node.PreviousNode = null;
}
node = previousNode;
}
// Not sure the whole logic of the above comparsion\equality?
}
Thanks
VulpesPosted Sep 16, 2013, 9:34 AM
So, this statement:
t.next = n.next;
would set the next node after 't' to null.
However, this statement:
t.next = n;
will insert the new node just after the node 't'.
The code within the next property of 't' will ensure that the next property of 'n' is set to whichever node followed 't' before the insertion.
Guest UserPosted Sep 16, 2013, 7:36 AM
Given the following:-
Node n;
n = new Node();
n.data = 1;
t.next = n;
Should the line
t.next = n;
be instead replaced with
t.next = n.next;
t.next = n; would mean t.next is set to point\reference the actual node rather than the nodes next variable?
Regards
Suthish NairPosted Sep 13, 2013, 7:51 AM
VulpesPosted Sep 12, 2013, 6:36 PM
To do this, each node's next node will become its previous node and vice versa.
As far as the root node is concerned this will now be at the other end of the list and so, as you iterate through the nodes, the root node is successively changed until the end of the list is reached.
I'd imagine that this method is part of a CustomDoubleLinkedList class and the RootNode property will probably be set when the first node is added to the list, rather than in the constructor.