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;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.