JAVA insertSorted insert an item into a given sorted linke

JAVA

   /**
   * insertSorted insert an item into a given sorted linked list (in
   * increasing order), so that the linked list remains sorted after the
   * insertion.
   *
   * Examples: elem : 0 and LinkedList : null ==> Resulting list : 0 --> null
   * elem : 1 and LinkedList : 2 --> 3 --> null ==> Resulting list : 1 --> 2
   * --> 3 --> null elem : -2 and LinkedList : -5 --> -4 --> null ==>
   * Resulting list : -5 --> -4 --> -2 --> null elem : 1 and LinkedList : -2
   * --> 1 --> 3 --> null ==> Resulting list : -2 --> 1 --> 1 --> 3 --> null
   */

Funtion name:

   public void insertSorted(int elem) {
       // TODO

Solution

ANSWER:

import java.util.LinkedList;

import java.util.Scanner;

public class LinkedListMain {

// Creating LinkedList of type Integer, as static. Cause only one copy for entire program

   private static LinkedList<Integer> linkedList;

// Insert metod for an element

private static void insertSorted(int elem) {

// If the LinkedList empty, add element at first position

if (linkedList.size() == 0) {

linkedList.add(elem); // Adds the element at 0th position

}

// Checking value with first element of an LinkedList

else if (linkedList.get(0) > elem) {

linkedList.add(0, elem); // Adds element at the first position

}

  // Checking value with last element of an LinkedList

else if (linkedList.get(linkedList.size() - 1) < elem) {

linkedList.add(linkedList.size(), elem); // Adds elements at the last position

}

// Above three if blocks fails, then this block will search for position where elem inserted

else {

int i = 0;

// This loop finds the position to insert an element

while (linkedList.get(i) < elem) {

i++;

}

linkedList.add(i, elem); // Insert the element at ith position

}

}

public static void main(String[] args) throws Exception {

Scanner sc = new Scanner(System.in); // Read the elements from key board

int option;

linkedList = new LinkedList<Integer>(); // Creating a new memory for LinkedList

insertSorted(2); // Calling insertSort() method to insert an element

insertSorted(3);

// This loop is to add and display elements into LinkedList

do {

System.out.print(\"1. Insert 2. Display \ Enter your choice: \");

option = sc.nextInt(); // Reading option from user

switch (option) {

case 1:

System.out.println(\"Enter a value to insert: \");

insertSorted(sc.nextInt()); // Reading element and insert into LinkedList

break;

case 2:

System.out.println(\"Elements: \" + linkedList); // Displaying the LinkedList

break;

default:

System.out.println(\"Please enter a valid option\");

}

System.out.print(\"Do you want to continue(y/n): \");

} while (sc.next().charAt(0) == \'y\'); // This loop will continue untill your enter \'y\' character

sc.close(); // closing the Scanner (optional)

}

}

Explanation: I am almost explan the code at each line as comments. Let me know any concerns. Thank you

JAVA /** * insertSorted insert an item into a given sorted linked list (in * increasing order), so that the linked list remains sorted after the * insertion. *
JAVA /** * insertSorted insert an item into a given sorted linked list (in * increasing order), so that the linked list remains sorted after the * insertion. *
JAVA /** * insertSorted insert an item into a given sorted linked list (in * increasing order), so that the linked list remains sorted after the * insertion. *

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site