In: Computer Science
This LinkedListUtil class tests various usages of the LinkedList class. The single param is an array of string. You will create a linked list with the string elements and return the linked list with all but the 1st two elements removed.
Note: In this question use a for or while loop instead of the suggested iterator. You should also ignore CodeCheck’s error message about a missing class (LinkedListUtil.class). Your code still needs to pass all test cases.
EDIT: you want to remove all besides the first 2 elements. So only the first 2 elements should be returned
LinkedListUtil.java
import java.util.LinkedList;
import java.util.ListIterator;
/**
This LinkedListUtil class tests various usages of the LinkedList
class
*/
public class LinkedListUtil
{
/**
Constructs a LinkedListUtil.
@param list is the initialized list
*/
public LinkedListUtil(LinkedList list)
{
this.list = list;
}
/**
deletes all but the first two linked list enries
*/
public void processList()
{
// TODO: create a list iterator and remove all but the first two
elements
}
private LinkedList list;
// this method is used to check your work
public static LinkedList check(String[] values)
   {  
LinkedList list = new LinkedList();
for (String s : values)
       list.addLast(s);
LinkedListUtil tester = new LinkedListUtil(list);
tester.processList();
return list;
}
}
processList(): this method keeps removing last element of the list until the size of list equals to 2. I have added main() in this class to run and test the method. LinkedListUtil.java
import java.util.LinkedList;
import java.util.ListIterator;
/**
 * This LinkedListUtil class tests various usages of the LinkedList class
 */
public class LinkedListUtil {
    /**
     * Constructs a LinkedListUtil.
     *
     * @param list is the initialized list
     */
    public LinkedListUtil(LinkedList list) {
        this.list = list;
    }
    /**
     * deletes all but the first two linked list enries
     */
// remove the last element from list until the size of list equals to 2
    public void processList() {
        while (list.size() > 2) {
            list.removeLast();
        }
    }
    private LinkedList list;
    // this method is used to check your work
    public static LinkedList check(String[] values) {
        LinkedList list = new LinkedList();
        for (String s : values)
            list.addLast(s);
        LinkedListUtil tester = new LinkedListUtil(list);
        tester.processList();
        return list;
    }
    public static void main(String[] args) {
        String[] values = new String[]{"Apple", "Banana", "Orange", "Kiwi", "Grapes", "Avocado"};
        LinkedList ll = LinkedListUtil.check(values);
        System.out.println("Testcase 1:");
        ll.stream().forEach(System.out::println);
        values = new String[]{"Football", "Hockey", "Baseball", "Cricket", "Rugby", "Tennis"};
        ll = LinkedListUtil.check(values);
        System.out.println("Testcase 2:");
        ll.stream().forEach(System.out::println);
    }
}
Screenshot:


Output: All the elements of the list are deleted except first two elements.
