4
Answers

Custom Doubly Linked List Implementation

S

S

11y
1.2k
1
Given the following code snippet, I need clarification on a Custom Doubly Linked List Implementation:-

I have placed comments under each line of code, please correct where necessary...

  public void ReverseList()
        {
            CustomDoubleLinkedListItem<T> node = this.RootNode.NextNode;
      // Get a reference to the current root nodes next node? what is this previously set to and how, constructor?

            CustomDoubleLinkedListItem<T> previousNode;
// declare a field named previous node

            CustomDoubleLinkedListItem<T> 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
Answers (4)