Question
Using the class Node developed in class (name it LLNode.java) that has the following methods: A method that allows the user to add a node
Using the class Node developed in class (name it LLNode.java) that has the following methods:
A method that allows the user to add a node to a linked list (use the non-default constructor - in the method ask the user for a value for description, name the method addNode)
A method that allows the user to remove a node from a linked list (name it deleteNode - in the method ask the user for a value for description, the node with that description should be removed).
A method that prints the value in description for each node in a linked list.
A main method that demonstrates the methods above.
public class Node { public String data; public Node next;
public Node() { data = ""; next = null; }//end method
public Node(String s) { data = s; next = null; }//end method
public String toString() { return "Data:" + data; }
}//end class
____________________________________________________________________________
import java.util.Scanner;
public class Demo_LinkedList { public static void main(String[] args) { Node p, q, r;
p = new Node("Mercury"); q = p;
r = new Node("Venus"); q.next = r; q = r;
r = new Node("Earth"); q.next = r; q = r;
r = new Node("Mars"); q.next = r; q = r;
r = new Node("Jupiter"); q.next = r; q = r;
r = new Node("Saturn"); q.next = r; q = r;
r = new Node("Uranus"); q.next = r; q = r;
r = new Node("Neptune"); q.next = r; q = r;
System.out.println(printLinkedListNodes(p)); removeNode(p); System.out.println(printLinkedListNodes(p)); }//end main
public static String printLinkedListNodes(Node nodePassed) { if(nodePassed.next == null) return nodePassed.data; else return nodePassed.data + " " + printLinkedListNodes(nodePassed.next); }//end method
public static void removeNode(Node nodePassed) { Scanner keyboard = new Scanner(System.in); String userInput; System.out.println("Enter the planet to remove:"); userInput = keyboard.nextLine(); Node currentNode = nodePassed; Node temp;
while(currentNode.next != null) { temp = currentNode.next.next; if(userInput.equalsIgnoreCase(currentNode.data)) currentNode.next = temp; currentNode = currentNode.next; }//end while
}//end method
}//end class
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