Question
Please use Java. Consider the following code: public class LinkedListDemo { public static void main(String[] args) { Node list = new Node(Salim); list.next = new
Please use Java.
Consider the following code:
public class LinkedListDemo {
public static void main(String[] args) {
Node list = new Node("Salim");
list.next = new Node("Alice");
Node p = list.next;
list.next = new Node("Bobby", p);
list = new Node("Vien", list);
}
}
class Node
{
String value;
Node next;
Node(String val, Node n)
{
value = val;
next = n;
}
Node(String val)
{
value = val;
next = null;
}
}
1. Draw a diagram showing the nodes in this list and value stored in each node.
2. Add a piece of code at the end of the main method to display the values stored in the list node from beginning to end. Test your code to verify your answer to question 1 above.
3. Write a method that takes a Node reference parameter and displays all the nodes in the linked list references at by the argument. If the list is empty, it outputs
4. Write a piece of code to remove the node with value Bobby from the list. Use the display method you wrote in exercise 3 to test and debug your code.
5. Write a piece of code to add a new node with value Megan: at the beginning of the list
6. Write a piece of code to add a new node with value Amy after the node with value Alice.
Demo this task by writing code to display the code at end of exercise 6.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started