Question: (Java Linked Lists) Create a method getAllPrefixes in class ListUtilities. Refer to the following test cases for specifics. @Test public void test_getAllPrefixes_01() { ListUtilities util
(Java Linked Lists) Create a method getAllPrefixes in class ListUtilities. Refer to the following test cases for specifics.
@Test public void test_getAllPrefixes_01() { ListUtilities util = new ListUtilities(); Node input = new Node<>(23, new Node<>(46, new Node<>(19, new Node<>(-9, null)))); /* * Given an input chain of integers, return a chain of strings. * * Each string in the output chain is a prefix (i.e., first few numbers) of the input chain, * whose length is between the specified lower and upper bounds (inclusive at both ends). * * Each prefix should be formatted as a comma-separated list of integers, surrounded by a pair of square brackets. * * Hints: * + The length of the output chain is equal to the length of the input chain. * + Elements in the output chain are ``sorted'' by the lengths of the prefixes * (e.g,. the first element is the prefix of length 1, the second element is the prefix of length 2). * * Assumptions: * + The input chain contains at least one node in it. * + For each call of getAllPrefixes(input, m, n): * * The input chain is not null and contains at least one node. * * m <= number of nodes in the input chain * * n <= number of nodes in the input chain * * m <= n */ /* * getAllPrefixes(23 -> 46 -> 19 -> -9, 1, 4) returns a chain of all prefixes whose lengths are between 1 and 4. */ Node output = util.getAllPrefixes(input, 1, 4); assertEquals("[23]", output.getElement()); assertEquals("[23, 46]", output.getNext().getElement()); assertEquals("[23, 46, 19]", output.getNext().getNext().getElement()); assertEquals("[23, 46, 19, -9]", output.getNext().getNext().getNext().getElement()); assertNull(output.getNext().getNext().getNext().getNext()); } @Test public void test_getAllPrefixes_02() { ListUtilities util = new ListUtilities(); Node input = new Node<>(23, new Node<>(46, new Node<>(19, new Node<>(-9, null)))); /* * getAllPrefixes(23 -> 46 -> 19 -> -9, 2, 3) returns a chain of all prefixes whose lengths are between 2 and 3. */ Node output = util.getAllPrefixes(input, 2, 3); assertEquals("[23, 46]", output.getElement()); assertEquals("[23, 46, 19]", output.getNext().getElement()); assertNull(output.getNext().getNext()); }