Question
This java program its supposed to find and print the smallest value in the linked list, i've been poking it for a couple of hours
This java program its supposed to find and print the smallest value in the linked list, i've been poking it for a couple of hours so I give up. I focused on the print list loop, but couldnt make it work.
public class IntNode { private int dataVal; // Node data private IntNode nextNodePtr; // Reference to the next node
public IntNode() { dataVal = 0; nextNodePtr = null; }
// Constructor public IntNode(int dataInit) { this.dataVal = dataInit; this.nextNodePtr = null; }
// Constructor public IntNode(int dataInit, IntNode nextLoc) { this.dataVal = dataInit; this.nextNodePtr = nextLoc; }
/* Insert node after this node. Before: this -- next After: this -- node -- next */ public void insertAfter(IntNode nodeLoc) { IntNode tmpNext;
tmpNext = this.nextNodePtr; this.nextNodePtr = nodeLoc; nodeLoc.nextNodePtr = tmpNext; return; }
// Get location pointed by nextNodePtr public IntNode getNext() { return this.nextNodePtr; }
public void printNodeData() { System.out.println(this.dataVal); return; } }
public class CustomLinkedList { public static void main (String[] args) { IntNode headObj; // Create IntNode objects IntNode currObj; IntNode lastObj; int i = 0; // Loop index headObj = new IntNode(-1); // Front of nodes list lastObj = headObj; for (i = 0; i < 20; ++i) { // Append 20 rand nums int rand = (int)(Math.random() * 100000); // random int (0-100000) currObj = new IntNode(rand); lastObj.insertAfter(currObj); // Append curr lastObj = currObj; } currObj = headObj; // Print the list while (currObj != null) { currObj.printNodeData(); currObj = currObj.getNext(); } return; } }
Current output
Compiling...done. Running...done.
-1
46604
28354
19390
60400
21801
34155
98985
74484
10274
86078
30342
92840
45005
48178
11462
15254
67261
18616
78667
46369
Step 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