import java.util.Scanner; /** * Question: * * Finish the implementation of the i
ID: 3691456 • Letter: I
Question
import java.util.Scanner;
/**
* Question:
*
* Finish the implementation of the insertAscending() method.
* This method will insert the given data into the list in a way that
* will keep the list in sorted ascending order.
* For example. Assuming you are starting with an empty list:
* insertAscending(1) would produce:
* 1
* insertAscending(5) would produce:
* 1 5
* insertAscending(3) would produce
* 1 3 5
* insertAscending(0) would produce:
* 0 1 3 5
* and insertAscending(6) would produce:
* 0 1 3 5 6
*
* Only add code in between the comments that say
* "Begin your code here"
* and
* "End your code here"
*/
public class LinkedList {
// Private inner class Node
private class Node{
int data;
Node link;
@SuppressWarnings("unused")
public Node(){
data = Integer.MIN_VALUE;
link = null;
}
public Node(int x, Node p){
data = x;
link = p;
}
}
// End of Node class
public Node head;
public LinkedList(){
head = null;
}
public void insertAscending(int data){
/** Begin your code here **/
/** end your code here **/
}
public void printList(){
Node position = head;
if (head == null)
System.out.println("The list is empty.");
while(position != null){
System.out.print(position.data + " ");
position = position.link;
}
System.out.println();
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
LinkedList list = new LinkedList();
System.out.println("How many values would you like to insert?");
int toInsert = input.nextInt();
for (int i = 0; i < toInsert; i++) {
System.out.println("Enter value " + (i + 1));
list.insertAscending(input.nextInt());
System.out.println("The list is: ");
list.printList();
System.out.println();
}
input.close();
}
}
Explanation / Answer
import java.util.Scanner;
public class LinkedList {
// Private inner class Node
private class Node{
int data;
Node link;
@SuppressWarnings("unused")
public Node(){
data = Integer.MIN_VALUE;
link = null;
}
public Node(int x, Node p){
data = x;
link = p;
}
}
// End of Node class
public Node head;
public LinkedList(){
head = null;
}
public void insertAscending(int data){
/** Begin your code here **/
if(head == null){
head = new Node(data, null);
return;
}
Node temp = head;
Node prev = null;
Node newNode = new Node(data, null);
while(temp!=null && temp.data < data){
prev = temp;
temp = temp.link;
}
if(temp == null){// insert newNode at last
prev.link = newNode;
}else if(prev == null){// insert at first
newNode.link = head;
head = newNode; // update head
}else{
newNode.link = temp;
prev.link = newNode;
}
/** end your code here **/
}
public void printList(){
Node position = head;
if (head == null)
System.out.println("The list is empty.");
while(position != null){
System.out.print(position.data + " ");
position = position.link;
}
System.out.println();
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
LinkedList list = new LinkedList();
System.out.println("How many values would you like to insert?");
int toInsert = input.nextInt();
for (int i = 0; i < toInsert; i++) {
System.out.println("Enter value " + (i + 1));
list.insertAscending(input.nextInt());
System.out.println("The list is: ");
list.printList();
System.out.println();
}
input.close();
}
}
/*
Sample run:
How many values would you like to insert?
5
Enter value 1
4
The list is:
4
Enter value 2
1
The list is:
1 4
Enter value 3
5
The list is:
1 4 5
Enter value 4
3
The list is:
1 3 4 5
Enter value 5
2
The list is:
1 2 3 4 5
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.