Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help writing a few methods in the Generic Node class....and if you could te

ID: 3797999 • Letter: N

Question

Need help writing a few methods in the Generic Node class....and if you could tell me if you see any issues with the methods I have already written, it would be appreciated

*addNodeAfter() instead of being void, returns the link it creates....just want to make sure my method header is correct and the return type (Node) is correct, or if i need Node<E>

*removeNodeAfter() returns the link it creates (that is the node after the removed element....just want to make sure my method header is correct and the return type (Node) is correct

* toString() creates and returns a string message which reveals the stored value(data) but not the link (non-recursive method documented below is the class, constructor, and a few methods i have written so far...this is part of a much larger project so if anything is unclear as far as specifications, I can provide it

CODE----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

public class Node<E>
{
// Invariant of the Node class:
// 1. Each node has one reference to an E Object, stored in the instance
// variable data.
// 2. For the final node of a list, the link part is null.
// Otherwise, the link part is a reference to the
// next node of the list.
private E data;
private Node<E> link;   

/**
* Initialize a node with a specified initial data and link to the next
* node. Note that the initialLink may be the null reference,
* which indicates that the new node has nothing after it.
* @param initialData
* the initial data of this new node
* @param initialLink
* a reference to the node after this new node--this reference may be null
* to indicate that there is no node after this new node.
* @postcondition
* This node contains the specified data and link to the next node.
**/   
public Node(E initialData, Node<E> initialLink)
{
data = initialData;
link = initialLink;
}

public Node addNodeAfter(E element)   
{
return link = new Node<E>(element, link);
}

public Node addNodeAfter(E element)   
{
return link = new Node<E>(element, link);
}

Any help is greatly apprecitated.

Explanation / Answer

//Please find the implementation below along with the tester class that I wrote to test the functions

PROGRAM CODE:

public class Node <E>{

   // Invariant of the Node class:

       // 1. Each node has one reference to an E Object, stored in the instance

       // variable data.

       // 2. For the final node of a list, the link part is null.

       // Otherwise, the link part is a reference to the

       // next node of the list.

       private E data;

       private Node<E> link;

       /**

       * Initialize a node with a specified initial data and link to the next

       * node. Note that the initialLink may be the null reference,

       * which indicates that the new node has nothing after it.

       * @param initialData

       * the initial data of this new node

       * @param initialLink

       * a reference to the node after this new node--this reference may be null

       * to indicate that there is no node after this new node.

       * @postcondition

       * This node contains the specified data and link to the next node.

       **/

       public Node(E initialData, Node<E> initialLink)

       {

          data = initialData;

          link = initialLink;

       }

         

          public Node<E> addNodeAfter(E element)

       {

              Node<E> newNode = new Node<E>(element, null);

              if(link == null)

              {

                  link = newNode;

              }

              else {

                  Node<E> temp = link;

                  while(temp.link != null)

                  {

                       temp = temp.link;

                  }

                  temp.link = newNode;

              }

              return newNode;

       }

         

         

       public Node<E> removeNodeAfter(E element)

       {

           Node<E> removedNode = null;

           Node<E> temp = link;

           if(data == element)

           {

               if(link != null)

               {

                   removedNode = new Node<E>(data, null);

                   data = link.data;

                   link = link.link;

               }

           }

           else

           {

               while(temp != null)

               {

                   if(temp.data == element)

                   {

                      

                       if(temp.link != null)

                       {

                              removedNode = new Node<E>(temp.data, null);

                           temp.data = temp.link.data;

                           temp.link = temp.link.link;

                       }

                       else

                           temp = null;

                       break;

                   }

                   temp = temp.link;

               }

           }

           return removedNode;

       }

         

       @Override

       public String toString() {

           String value;

           Node<E> temp = link;

           value = String.valueOf(data) + " ";

           while(temp != null)

           {

               value += String.valueOf(temp.data) + " ";

               temp = temp.link;

           }

           return value;

       }

}

package array;

public class NodeTester {

   public static void main(String[] args) {

       Node<Integer> node1 = new Node<Integer>(0, null);

       node1.addNodeAfter(2);

       node1.addNodeAfter(4);

       node1.addNodeAfter(6);

       node1.removeNodeAfter(4);

       System.out.println(node1);

   }

}

OUTPUT:

0

2

6

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote