Answered step by step
Verified Expert Solution
Question
1 Approved Answer
LAB: Sorted number list implementation with linked lists Step 1 : Inspect the Node.java file Inspect the class declaration for a doubly - linked list
LAB: Sorted number list implementation with linked lists
Step : Inspect the Node.java file
Inspect the class declaration for a doublylinked 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 : Implement the insert method
A class for a sorted, doublylinked 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 then insert is called. A new node with data value is created and inserted between and thus preserving the list's sorted order and yielding:
Step : Test in develop mode
Code in main takes a spaceseparated list of numbers and inserts each into a SortedNumberList. The list is displayed after each insertion. Ex: If input is
then output is:
List after inserting :
List after inserting :
List after inserting :
List after inserting :
Try various program inputs, ensuring that each outputs a sorted list.
Step : 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 commentedout 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
then output is:
List after inserting :
List after inserting :
List after inserting :
List after inserting :
List after removing :
List after removing :
LabProgram.java
import java.util.Scanner;
public class LabProgram
Prints the SortedNumberList's contents, in order from head to tail
public static void printsListSortedNumberList list
Node node list.head;
while node null
System.out.printnodegetData;
node node.getNext;
System.out.println;
public static void mainString args
Scanner scnr new ScannerSystemin;
String inputLine;
SortedNumberList list new SortedNumberList;
Read the first line of inputs and insert each number into the list
inputLine scnrnextLine;
String inputs inputLine.split;
for String input : inputs
double number Double.parseDoubleinput;
list.insertnumber;
printListlist;
Read the second line of inputs and remove each number from the list
if scnrhasNextLine
inputLine scnrnextLine;
inputs inputLine.split;
for String input : inputs
double number Double.parseDoubleinput;
list.removenumber;
printListlist;
scnrclose;
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 Nodedouble initialData
data initialData;
next null;
previous null;
Constructs this node with the specified numerical data value, references to the next
and previous nodes.
public Nodedouble 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 setDatadouble newData
data newData;
Gets the reference to the next node.
public Node getNext
return next;
Sets the reference to the next node.
public void setNextNode newNext
next newNext;
Gets the reference to the previous node.
public Node getPrevious
return previous;
Sets the reference to the previous node.
public void setPreviousNode newPrevious
previous newPrevious;
SortedNumberList.java
public class SortedNumberList
java please
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