Package arraylist1 import javautilLinkedList Publis class Re

Package arraylist1; import java.util.LinkedList; Publis class RemoveFirstLast {public static void main(String[] args) {LinkedList 1List = new LinkedList(); 1List.add(\"1\"); 1List.add(\"2\"); 1List.add(\"3\"); 1List.add(\"4\"); 1List.add(\"5\"); System.out.println(\"LinkedList contains; + 1List); Object object - 1List.removeFirst(); System.out.println(object + has been removed from the first index of LinkedList\"); System.out.println(\"LinkedList now contain; + 1List); object - 1List.removeLast(); System.out.println(object + has been removed from the last index of LinkedList\"); System.out.println(\"LinkedList now contains: + 1List);}} Using the above code as a reference along with any other references you wish to use as a team, create a Java program that adds or removes an item from a linked list based on user input. You can use the index of the entry as the key to identify the record for addition or deletion. The program can be as simple as you like but must add an entry to the list based on the user input and also must delete the proper item in the list based on the index entered by the user. Display your list contents after each operation.

Solution

RemoveItemsFromList.java

import java.util.Scanner;
import java.util.LinkedList;

public class RemoveItemsFromList {

  
   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       LinkedList<String> list = new LinkedList<String>();
       for(;;){
           System.out.print(\"Enter the string to add in list (quit to exit): \");
           String s = scan.nextLine();
           if(s.equalsIgnoreCase(\"quit\")){
               break;
           }
           list.add(s);
       }
       System.out.println(\"List items are \"+list);
       System.out.print(\"Enter the index to delete the item from list: \");
       int index = scan.nextInt();
       list.remove(index);
       System.out.println(\"List items after remove the item are \"+list);
      

   }

}

Output:

Enter the string to add in list (quit to exit): 1
Enter the string to add in list (quit to exit): 2
Enter the string to add in list (quit to exit): 3
Enter the string to add in list (quit to exit): 4
Enter the string to add in list (quit to exit): 5
Enter the string to add in list (quit to exit): quit
List items are [1, 2, 3, 4, 5]
Enter the index to delete the item from list: 3
List items after remove the item are [1, 2, 3, 5]

 Package arraylist1; import java.util.LinkedList; Publis class RemoveFirstLast {public static void main(String[] args) {LinkedList 1List = new LinkedList(); 1Li

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site