Code in main() takes a space-separated list of numbers and inserts each into a Sorted NumberList. The list is displayed after each insertion. Ex: If input is 77 15 42 63.5 then output is: List after inserting 77: 77 List after inserting 15: 15 77 List after inserting -42: -42 15 77 List after inserting 63.5: -42 15 63.5 77 Try various program inputs, ensuring that each outputs a sorted list.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

I need help with this Java problem to output as it's explained in this image below:

 

Ex: If input is

84 72 19 61

19 84

 

then output is:

List after inserting 84: 

84 

List after inserting 72: 

72 84 

List after inserting 19: 

19 72 84 

List after inserting 61: 

19 61 72 84 

List after removing 19: 

61 72 84 

List after removing 84: 

61 72

 

 

LabProgram.java code below (Needs editing)

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 code below (Don't edit)

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 code below (Needs editing)

 

public class SortedNumberList {

public Node head;

public Node tail;

 

public SortedNumberList() {

head = null;

tail = null;

}

 

   // Optional: Add any desired private methods here

   

   

// Inserts the number into the list in the correct position such that the

// list remains sorted in ascending order.

public void insert(double number) {

// Your code here

}

 

// Removes the node with the specified number value from the list. Returns

// true if the node is found and removed, false otherwise.

public boolean remove(double number) {

// Your code here

   

return false;

}

}

Code in main() takes a space-separated list of numbers and inserts each into a Sorted NumberList. The list is displayed after each insertion.
Ex: If input is
77 15 -42 63.5
then output is:
List after inserting 77:
77
List after inserting 15:
15 77
List after inserting -42:
-42 15 77
List after inserting 63.5:
-42 15 63.5 77
Try various program inputs, ensuring that each outputs a sorted list.
Transcribed Image Text:Code in main() takes a space-separated list of numbers and inserts each into a Sorted NumberList. The list is displayed after each insertion. Ex: If input is 77 15 -42 63.5 then output is: List after inserting 77: 77 List after inserting 15: 15 77 List after inserting -42: -42 15 77 List after inserting 63.5: -42 15 63.5 77 Try various program inputs, ensuring that each outputs a sorted list.
Expert Solution
steps

Step by step

Solved in 4 steps with 8 images

Blurred answer
Knowledge Booster
Problems on Dynamic Programming
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education