Exercise 2- Singly Linked Lists 1. Open LinearNode.java and BuildLinkedList.java in Eclipse and examine the code in both

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Exercise 2- Singly Linked Lists 1. Open LinearNode.java and BuildLinkedList.java in Eclipse and examine the code in both

Post by answerhappygod »

Exercise 2 Singly Linked Lists 1 Open Linearnode Java And Buildlinkedlist Java In Eclipse And Examine The Code In Both 1
Exercise 2 Singly Linked Lists 1 Open Linearnode Java And Buildlinkedlist Java In Eclipse And Examine The Code In Both 1 (260.7 KiB) Viewed 69 times
public class LinearNode<E>{ private LinearNode<E> next; private E element; /** * Creates an empty node. */ public LinearNode() { next = null; element = null; } /** * Creates a node storing the specifiedelement. * * @param elem the element to bestored within the new node */ public LinearNode (E elem) { next = null; element = elem; } /** * Returns the node that follows thisone. * * @return the node that follows thecurrent one */ public LinearNode<E> getNext() { return next; } /** * Sets the node that follows thisone. * * @param node the node to be set tofollow the current one */ public void setNext (LinearNode<E>node) { next = node; } /** * Returns the element stored in thisnode. * * @return the element stored in thisnode */ public E getElement() { return element; } /** * Sets the element stored in thisnode. * * @param elem the element to bestored in this node */ public void setElement (E elem) { element = elem; }}
-------
public class BuildLinkedList {
/* * Print the information stored in all the nodesof the list whose first node is * referenced by front */ private static voidprintList(LinearNode<Integer> front) { LinearNode<Integer>current = front; for (int i = 1; i <= 10;i++) { System.out.println(current.getElement()); current =current.getNext(); } }
public static void main(String[] args) {
// create a linked listthat holds 1, 2, ..., 10 // by starting at 10 andadding each node at head of list
LinearNode<Integer>front = null; // create empty linked list LinearNode<Integer>intNode;
for (int i = 10; i >=1; i--) { // createa new node for i intNode =new LinearNode<Integer>(new Integer(i)); // add itat the head of the linked list intNode.setNext(front); front =intNode; }
printList(front); }
}
Exercise 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 BuildLinked List 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 nodes.
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply