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:

3. Consider the LinkedList class with an inner Node class as following:

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 head

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!