Question: Problem 3 Difficulty: MEDIUM Problem Statement: Develop a Java program that implements a basic singly linked list and provides a user interface for interacting with

Problem 3
Difficulty: MEDIUM
Problem Statement:
Develop a Java program that implements a basic singly linked list and provides a user
interface for interacting with the list. The program should include three classes: Node,
LinkedList & Main.
The Node class should have two properties: data (an integer) and next (a reference to
the next Node in the list). The Node class should also have a constructor that accepts
an integer, which is used to initialize the data property, and sets the next property to
null.
The LinkedList class should have one property: head (a reference to the first Node in
the list). The LinkedList class should also have a constructor that initializes the head
property to null.
The LinkedList class should include three methods: add, remove, and display.
The add method should accept an integer, create a new Node with that integer, and
add the new Node to the end of the list. If the list is empty (i.e., head is null), the new
Node should become the head of the list. Otherwise, the method should iterate through
the list until it finds the last Node, and then set the next property of the last Node to the
new Node.
2024-02-15
6/10
The remove method should accept an integer and remove the first Node in the list that
contains that integer. If the list is empty (i.e., head is null), the method should do
nothing. If the head of the list contains the integer, the head should be set to the next
Node in the list. Otherwise, the method should iterate through the list until it finds the
Node that contains the integer and then remove that Node from the list. On deleting all
the nodes in an existing linked list the head will point to null just as it was when the
Linked list was initialised. Printing the linked list in that case will print nothing.
The display method should iterate through the list and print the data of each Node to
the console. If the list is empty (i.e., head is null), the method should print a specific
message indicating that the list is empty.
In the main method in class Main, a menu should be displayed to the user with the
following options: Add, Remove, Display, and Exit. The user should be prompted to
enter a choice. Depending on the user's choice, the corresponding operation should be
performed on the list. If the user chooses Exit, the program should terminate. If the
user enters an invalid choice, a specific message should be displayed and the user
should be prompted to enter a choice again.
Code Structure
Use this syntax for writing your solution. You cannot add any more classes in the
structure given.
import java.util.Scanner;
class Node {
}
class LinkedList {
Node head;
public LinkedList(){
}
public void add(int data){
}
public void remove(int data){
}
public void display(){
}
}
public class Main {
public static void main(String[] args){
LinkedList list = new LinkedList();
Scanner scanner = new Scanner(System.in);
while (true){
// Menu:
//1. Add
//2. Remove
//3. Display
//4. Exit
int choice = scanner.nextInt();
switch (choice){
case 1:
int valueToAdd = scanner.nextInt();
list.add(valueToAdd);
break;
case 2:
int valueToRemove = scanner.nextInt();
list.remove(valueToRemove);
break;
case 3:
list.display();
break;
case 4:
System.out.println("Exiting...");
scanner.close();
System.exit(0);
default:
System.out.println("Invalid choice. Please try again.");
}
}
}
}
NOTE
You may only make use of the following packages wherever necessary, no other
package is allowed:
java.util.Scanner
Sample Test Cases
Input:
1
12
1
34
2
12
3
4
Output:
34
Exiting...
Please solve this and please give full code with output. when i run the code it should show the correct output

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!