Answered step by step
Verified Expert Solution
Question
1 Approved Answer
JAVA. Please follow all instruction and use LinkedIntList class. Below is the classes for LinkedIntList and ListNode. Please include explanations for every statement. Thank you
JAVA. Please follow all instruction and use LinkedIntList class. Below is the classes for LinkedIntList and ListNode. Please include explanations for every statement. Thank you
public class ListNode { public int data; // data stored in this node public ListNode next; // link to next node in the list // post: constructs a node with given data and given link public ListNode(int data, ListNode next) { this.data = data; this.next = next; } // post: constructs a node with given data and null link public ListNode(int data) { this(data, null); } // post: constructs a node with data 0 and null link public ListNode() { this(0, null); } }
Below is LinkedIntList() --------------------------------------------------------------
public class LinkedIntList implements IntList { private ListNode front; // first value in the list // post: constructs an empty list public LinkedIntList() { front = null; } // post: manipulates the LinkedIntList by swapping the values in a // pair-wise style. Skips a last null value public void switchPairs() // exercise 8 { ListNode current = front; // List will start iterating from front // of the list, making current variable front. while (current.next != null) // until preceding node of current node // IS null, iterate { int temp = current.next.data; // create a temp of the preceding data current.next.data = current.data; // make preceding data equal to // current data current.data = temp; // current data is temp value, which is the value // of a preceding data current = current.next.next; // now iterate from a "non-swapped" value if (current == null) // if end of the list is reached, { break; // break the while loop } } } // post: returns the current number of elements in the list public int size() { int count = 0; ListNode current = front; while (current != null) { current = current.next; count++; } return count; } // pre : 0Write a method called split that rearranges the elements of a list so that all of the negative values appear before all of the nonnegatives. For example, suppose a variable list stores the values [8, 7, -4, 19, 0, 43, -8, -7, 2]. The call of list.split(); should rearrange the list to put the negatives first: [-4, -8, -7, 8, 7, 19, 0, 43, 2] . It doesn't matter what order the numbers are in, only that the negatives appear before the nonnegatives, so this is only one possible solution. You must solve the problem by rearranging the links of the list, not by swapping data values or creating new nodes. You also may not use auxiliary structures like arrays or ArrayLists to solve this
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