Question
3. Consider the LinkedList class with an inner Node class as following: public class LinkedList { private Node head; private class Node{ private T data;
3. Consider the LinkedList class with an inner Node class as following:
public class LinkedList
private Node head;
private class Node{
private T data;
private Node next;
public Node(T d) {
data = d;
next = null;
}
}
}
A linked list of integers created from the above List class is represented by the following diagram:
a. What is curNode.data after the following code segment is executed? (5 points)
Node curNode = head;
curNode = curNode.next.next;
b. What will be displayed after the following code segment is executed? (5 points)
Node curNode = head;
int n = 0;
while(curNode!= null){
n + = curNode.data;
curNode = curNode.next;
}
System.out.println(n= + n);
c. Draw a diagram of the above list after the following lines of code have been executed (5 points)
Node newNode = new Node(6);
Node curNode = head.next; newNode.next = curNode.next;
curNode.next = newNode;
d. In addition to the code above, assume the following code executes. Draw a diagram of the list after this code executes. (5 points)
Node curNode = head; curNode = curNode.next;
curNode = curNode.next;
Node nextNode= curNode.next;
curNode.next = nextNode.next;
nextNode = null;
7 3 4 8 headStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started