Question: Please give me the correct SortedNumberList.java * LabProgram.java import java.util.Scanner; import java.io.PrintWriter; public class LabProgram { // Prints the SortedNumberList's contents, in order from head

Please give me the correct SortedNumberList.java

Please give me the correct SortedNumberList.java * LabProgram.java import java.util.Scanner; import java.io.PrintWriter;public class LabProgram { // Prints the SortedNumberList's contents, in order fromhead to tail public static void printList(SortedNumberList list) { Node node =

* LabProgram.java

import java.util.Scanner; import java.io.PrintWriter;

public class LabProgram { // Prints the SortedNumberList's contents, in order from head to tail public static void printList(SortedNumberList list) { Node node = list.head; while (null != node) { System.out.print(node.getData() + " "); node = node.getNext(); } System.out.println(); }

public static void main(String[] args) { Scanner scnr = new Scanner(System.in); String inputLine;

// Read the line of input numbers inputLine = scnr.nextLine();

// Split on space character String[] terms = inputLine.split(" ");

// Insert each value and show the sorted list's contents after each insertion SortedNumberList list = new SortedNumberList();

for (Object term : terms) { double number = Double.parseDouble(term.toString()); System.out.println("List after inserting " + number + ": "); list.insert(number); printList(list); }

/* // Read the input line with numbers to remove inputLine = scnr.nextLine(); terms = inputLine.split(" ");

for (Object term : terms) { double number = Double.parseDouble(term.toString()); System.out.println("List after removing " + number + ": "); list.remove(number); printList(list); } */ } }

* Node.java

public class Node { protected double data; protected Node next; protected Node previous;

// Constructs this node with the specified numerical data value. References to the next // and previous nodes are assigned null. public Node(double initialData) { data = initialData; next = null; previous = null; }

// Constructs this node with the specified numerical data value, references to the next // and previous nodes. public Node(double initialData, Node nextNode, Node previousNode) { data = initialData; next = nextNode; previous = previousNode; }

// Returns this node's data. public double getData() { return data; }

// Sets this node's data. public void setData(double newData) { data = newData; }

// Gets the reference to the next node. public Node getNext() { return next; }

// Sets the reference to the next node. public void setNext(Node newNext) { next = newNext; }

// Gets the reference to the previous node. public Node getPrevious() { return previous; }

// Sets the reference to the previous node. public void setPrevious(Node newPrevious) { previous = newPrevious; } }

* SortedNumberList.java

public class SortedNumberList { protected Node head; protected Node tail;

// Initializes an empty SortedNumberList public SortedNumberList() { head = null; tail = null; }

public void insert(double value) { Node newNode = new Node(value); if (head == null) { head = newNode; tail = newNode; return; } Node current = head; while (current != null && current.getData()

public boolean remove(double value) { Node current = head; while (current != null && current.getData() != value) { current = current.getNext(); } if (current == null) { return false; } if (current.getPrevious() == null) { head = current.getNext(); head.setPrevious(null); } else if (current.getNext() == null) { tail = current.getPrevious(); tail.setNext(null); } else { Node previous = current.getPrevious(); Node next = current.getNext(); previous.setNext(next); next.setPrevious(previous); } return true; } }

problem

More insertion and removal (negative numbers, removal until list is empty, and so on)

java.lang.NullPointerException

4.18 LAB: Sorted number list implementation with linked lists Step 1: Inspect the Node.java file Inspect the class declaration for a doubly-linked list node in Node.java. Access Node.java by clicking on the orange arrow next to LabProgram.java at the top of the coding window. The Node class has three fields: - a double data value, - a reference to the next node, and - a reference to the previous node. Each field is protected. So code outside of the class must use the provided getter and setter methods to get or set a field. Node.java is read only, since no changes are required. Step 2: Implement the insert() method A class for a sorted, doubly-linked list is declared in SortedNumberList.java. Implement the SortedNumberList class's insert() method. The method must create a new node with the parameter value, then insert the node into the proper sorted position in the linked list. Ex: Suppose a SortedNumberList's current list is 2347.2586, then insert (33.5) is called. A new node with data value 33.5 is created and inserted between 23 and 47.25, thus preserving the list's sorted order and yielding: 2335.547.2586 Step 3: lest in develop mode Code in main() takes a space-separated list of numbers and inserts each into a SortedNumberList. The list is displayed after each insertion. Ex: If input is 77154263.5 then output is: List after inserting 77 : 77 List after inserting 15: 1577 List after inserting 42 : 421577 List after inserting 63.5 : 421563.577 Try various program inputs, ensuring that each outputs a sorted list. Step 4: Implement the remove( method Implement the SortedNumberList class's remove( method. The method takes a parameter for the number to be removed from the list. If the number does not exist in the list, the list is not changed and false is returned. Otherwise, the first instance of the number is removed from the list and true is returned. Uncomment the commented-out part in main() that reads a second input line and removes numbers from the list. Test in develop mode to ensure that insertion and removal both work properly, then submit code for grading. Ex: If input is 841972841961 then output is: List after inserting 84 : 84 List after inserting 72 : 7284 List after inserting 19 : 197284 List after inserting 61 : 19617284 List after removing 19: 617284 List after removing 84: 6172

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!