Question: Multiple integers are read from input and inserted into a linked list of DataNodes. For each negative integer found in the linked list of DataNodes,
Multiple integers are read from input and inserted into a linked list of DataNodes. For each negative integer found in the linked list of DataNodes, output the negative integer followed by " is invalid." on a new line.
Ex: If the input is 2 28 -1, then the output is:
-1 is invalid.
DataLinked.Java
import java.util.Scanner;
public class DataLinkedList { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); DataNode headData = null; DataNode currData = null; DataNode lastData = null; int count; int inputValue; int i; count = scnr.nextInt(); headData = new DataNode(count); lastData = headData;
for (i = 0; i < count; ++i) { inputValue = scnr.nextInt(); currData = new DataNode(inputValue); lastData.insertAfter(currData); lastData = currData; }
/* Your code goes here */ } }
Datanode.java
class DataNode { private int dataVal; private DataNode nextNodeRef;
public DataNode(int dataInit) { this.dataVal = dataInit; this.nextNodeRef = null; }
public void insertAfter(DataNode nodeLoc) { DataNode tmpNext = null;
tmpNext = this.nextNodeRef; this.nextNodeRef = nodeLoc; nodeLoc.nextNodeRef = tmpNext; }
public DataNode getNext() { return this.nextNodeRef; }
public int getNodeData() { return this.dataVal; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
