Question: 1 . Given the codes below, add code to prompt the user to enter how many new names they wish to add to the ArrayList

1. Given the codes below, add code to prompt the user to enter how many new names they wish to add to the ArrayList object and add it to the list. Display the updated list and its updated size to the console. CustomLinkedList.java //Example code to add multiple nodes to a singly linked list
public class CustomLinkedList {
public static void main(String[] args){
IntNode headObj; // Create IntNode reference variables
IntNode currObj; // tmp_ptr traverses the list
IntNode node; // new node to add the linked list
// write code to add 15 random numbers between 10 and 15 to
// the linked list
int counter =0;
// step 1: set the first node in the list
headObj = new IntNode((int)((Math.random()*5)+10));
// step 2: set tmp_ptr to the first node
currObj = headObj;
// until the tmp_ptr is null
while(currObj != null && counter <15){
// build the node structure
node = new IntNode((int)((Math.random()*5)+10));
// connect the node to the list
// is there no list?
if(headObj.getNext()== null){
headObj.insertAfter(node);
}
else{// is there a list?
currObj = currObj.getNext();
currObj.insertAfter(node);
}
counter++; // update counter
}
// Print linked list
currObj = headObj;
while (currObj != null){
currObj.printNodeData();
currObj = currObj.getNext();
}
}
} public class IntNode {
private int dataVal;
// Node data
private IntNode nextNodeRef;
// Reference to the next node
public IntNode(){
dataVal =0;
nextNodeRef = null;
}
// Constructor
public IntNode(int dataInit){
this.dataVal = dataInit;
this.nextNodeRef = null;
}
/* Insert node after this node. Before: this -- next After: this -- node -- next */
public void insertAfter(IntNode nodeLoc){
IntNode tmpNext;
tmpNext = this.nextNodeRef;
this.nextNodeRef = nodeLoc;
nodeLoc.nextNodeRef = tmpNext;
}
// Get location of nextNodeRef
public IntNode getNext(){
return this.nextNodeRef;
}
public void printNodeData(){
System.out.println(this.dataVal);
}
}

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!