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

The \"list\" has a integer \"position\". In an array, the position is very easy

ID: 3552407 • Letter: T

Question

The "list" has a integer "position". In an array, the position is very easy to implement as it is related to the "index" of the array. It is also much easier to retrieve the element of the node beause it is just the location that the index refers to. In the "linked list", position is much more difficult. Suppose you had a "head" of type "node" and an integer named "position".


You want to print the ist in a nice way so that you can make sure your program is working. You want your output to look like this:


head = ?


Position Element Next

1 ??? ???

2 ??? ???


Write your code below to implement "list" to print in the format above (in Java).


// asume head is a class instance variable

public void print()

{

// your code here....


}

Explanation / Answer

public void print()

{

   Node temp = head;

     

       int i , j =1;


      System.out.println("Position Element Next");


      while(temp != null)

      {

              if(temp.next != null)

              System.out.println( j + temp.data + temp.next.data);


             else

             System.out.println( j + temp.data + "NULL");


            j++;

            temp = temp.next;

        }

}

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