Question

In: Computer Science

This LinkedListUtil class tests various usages of the LinkedList class. The single param is an array...

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.

Java code

Complete the following file:

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<String> 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<String> list;

// this method is used to check your work
public static LinkedList<String> check(String[] values)
   {  
LinkedList<String> list = new LinkedList<String>();
for (String s : values)
       list.addLast(s);

LinkedListUtil tester = new LinkedListUtil(list);
tester.processList();
return list;
}

}

Solutions

Expert Solution

Program Code Screenshot :

Sample Output :

Program Code to Copy

import java.util.Iterator;
import java.util.LinkedList;
/**
 This LinkedListUtil class tests various usages of the LinkedList class
 */
class LinkedListUtil
{
    /**
     Constructs a LinkedListUtil.
     @param list is the initialized list
     */
    public LinkedListUtil(LinkedList<String> 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
        //Create an iterator
        Iterator i = this.list.iterator();
        //Skip 2 elements
        i.next();
        i.next();
        //Remove all the other elements
        while (i.hasNext()){
            i.next();
            i.remove();
        }
    }
    private LinkedList<String> list;
    // this method is used to check your work
    public static LinkedList<String> check(String[] values)
    {
        LinkedList<String> list = new LinkedList<String>();
        for (String s : values)
            list.addLast(s);

        LinkedListUtil tester = new LinkedListUtil(list);
        tester.processList();
        return list;
    }

}

class Main{
    public static void main(String[] args) {
        //Invoke the function with the Strings "a","b","c" and "d"
        LinkedList<String> linkedList = LinkedListUtil.check(new String[]{"a","b","c","d","e","f"});
        System.out.println(linkedList);
    }
}

Related Solutions

This LinkedListUtil class tests various usages of the LinkedList class. The single param is an array...
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: For clarifcation...
This LinkedListUtil class tests various usages of the LinkedList class. The single param is an array...
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...
What is the difference between Array and Linkedlist. What is Array with example? What is Linkedlist...
What is the difference between Array and Linkedlist. What is Array with example? What is Linkedlist with example? What is the difference?
(using single linkedlist c++)In this assignment, you will implement a Polynomial linked list(using single linkedlist only),...
(using single linkedlist c++)In this assignment, you will implement a Polynomial linked list(using single linkedlist only), the coefficients and exponents of the polynomial are defined as a node. The following 2 classes should be defined. p1=23x 9 + 18x 7+3 1. Class Node ● Private member variables: coefficient (double), exponents (integer), and next pointer. ● Setter and getter functions to set and get all member variables ● constructor 2. Class PolynomialLinkedList ● Private member variable to represent linked list (head)...
In java write a method that will take an array and change it into a linkedlist...
In java write a method that will take an array and change it into a linkedlist and then display it in the main method
1. (10 pts) Define the nodes in the LinkedList. Create the LinkedList using the ListNode class....
1. (10 pts) Define the nodes in the LinkedList. Create the LinkedList using the ListNode class. Create a method to find a node with given value in a LinkedList. Return the value is this value exists in the LinkedList. Return null if not exists. Use these two examples to test your method. Example 1: Input: 1 -> 2 -> 3, and target value = 3 Output: 3 Example 2: Input: 1 -> 2 -> 3, and target value = 4...
Consider the following definition of a doubly linked-list: class LinkedList{ public: LinkedList():head(0), tail(0){} ~LinkedList(); void reverse();...
Consider the following definition of a doubly linked-list: class LinkedList{ public: LinkedList():head(0), tail(0){} ~LinkedList(); void reverse(); //reverses the order of elements in the linked list void insert(int value); private: struct Node{ int data; Node* next; Node* prev; }; Node* head; Node* tail; //Add your helper function here that recursively reverses the order of elements in the linked list }; Write the declaration of a helper function in the class provided above that recursively reverses the order of elements in the...
Laboratory Tasks public class LinkedList {              Node head;               class Node {    &nbsp
Laboratory Tasks public class LinkedList {              Node head;               class Node {                       int data;                     Node next;                     Node(int d) {                             data = d;                             next = null;                     }                 } } Complete the above java program by adding the following methods: Part1: isEmpty() checks if the linked list is empty or not. (return type is boolean) printList() prints all data in the linked list. (void method) insertFirst(int newData) add newData at the head of the linked list. (void method) insertLasL(int newData) add newData at...
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class...
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class Queue{ public Queue(){ // use the linked list } public void enqueue(int item){ // add item to end of queue } public int dequeue(){ // remove & return item from the front of the queue } public int peek(){ // return item from front of queue without removing it } public boolean isEmpty(){ // return true if the Queue is empty, otherwise false }...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class Stack{ public Stack(){ // use LinkedList class } public void push(int item){ // push item to stack } public int pop(){ // remove & return top item in Stack } public int peek(){ // return top item in Stack without removing it } public boolean isEmpty(){ // return true if the Stack is empty, otherwise false } public int getElementCount(){ // return current number...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT