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.

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 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;
}

}

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks


//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() {
                // looping as long as list is not null and list has more than 2
                // elements.
                // note: I am using while loop instead of an iterator as mentioned in
                // the question description.
                while (list != null && list.size() > 2) {
                        // removing last element
                        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;
        }

}

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. 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...
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...
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...
Write a C++ class that implement two stacks using a single C++ array. That is, it...
Write a C++ class that implement two stacks using a single C++ array. That is, it should have functions pop_first(), pop_second(), push_first(…), push_second(…), size_first(), size_second(), …. When out of space, double the size of the array (similarly to what vector is doing). Notes: Complete all the functions in exercise_2.cpp, then submit this cpp file. pop_first() and pop_second() should throw std::out_of_range exception when stack is empty. CODE: #include <cstdio> #include <stdexcept> template <class T> class TwoStacks { public:   // Constructor, initialize...
Create a concrete LinkedList class that extends the provided ALinkedList class. You will need to override...
Create a concrete LinkedList class that extends the provided ALinkedList class. You will need to override the extract()method in your class. You can use your main() method for testing your method (although you do not need to provide a main method). Recall that a list is an ordered collection of data X_0, X_1, X_2, ..., X_n-1 The extract(int start, int end) method removes all elements X_start, X_start_1, ..., X_end-1 from the list. It also returns all removed elements as a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT