Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Linearnode.java file: /** * LinearNode represents a node in a linked list. * * @author Dr. Lewis * @author Dr. Chase * @version 1.0, 08/13/08
Linearnode.java file:
/** * LinearNode represents a node in a linked list. * * @author Dr. Lewis * @author Dr. Chase * @version 1.0, 08/13/08 */ public class LinearNode{ private LinearNode next; private E element; /** * Creates an empty node. */ public LinearNode() { next = null; element = null; } /** * Creates a node storing the specified element. * * @param elem the element to be stored within the new node */ public LinearNode (E elem) { next = null; element = elem; } /** * Returns the node that follows this one. * * @return the node that follows the current one */ public LinearNode getNext() { return next; } /** * Sets the node that follows this one. * * @param node the node to be set to follow the current one */ public void setNext (LinearNode node) { next = node; } /** * Returns the element stored in this node. * * @return the element stored in this node */ public E getElement() { return element; } /** * Sets the element stored in this node. * * @param elem the element to be stored in this node */ public void setElement (E elem) { element = elem; } }
BuildLinkedList.java file:
/** * Build a linked list of integers from 1 to 10 * * @author CS1027 */ public class BuildLinkedList { /* * Print the information stored in all the nodes of the list whose first node is * referenced by front */ private static void printList(LinearNodeExercise 2 - Singly Linked Lists 1. Open LinearNode.java and BuildLinkedList.java in Eclipse and examine the code in both classes. 2. The main() method in BuildLinkedList created a list with 10 nodes, each storing an integer value from 1 to 10. The printList() method prints out this list. Run the program to see the list elements printed out. 3. The problem is that printList() is "hard-coded" to loop through 10 elements in the for-loop so this method will not work for any other size of list. Modify this method so that it works for any list length (i.e. not hard-coded). You cannot change the method signature (parameters). You also cannot add instance variables to the class. 4. In the main() method, change the for-loop to create a list of 20 nodes and run the program to ensure that it correctly creates and prints out those 20 nodes. 5. Change it again, this time to 7 and check that it works properly with the 7 nodesfront) { LinearNode current = front; for (int i = 1; i front = null; // create empty linked list LinearNode intNode; for (int i = 10; i >= 1; i--) { // create a new node for i intNode = new LinearNode (new Integer(i)); // add it at the head of the linked list intNode.setNext(front); front = intNode; } printList(front); } }
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