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?
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...
import java.util.LinkedList; public class StudentLinkedList { public static void main(String[] args) { LinkedList<Student> linkedlist = new...
import java.util.LinkedList; public class StudentLinkedList { public static void main(String[] args) { LinkedList<Student> linkedlist = new LinkedList<Student>(); linkedlist.add(new Student("Ahmed Ali", 20111021, 18, 38, 38)); linkedlist.add(new Student("Sami Kamal", 20121021, 17, 39, 35)); linkedlist.add(new Student("Salem Salim", 20131021, 20, 40, 40)); linkedlist.add(new Student("Rami Mohammed", 20111031, 15, 35, 30)); linkedlist.add(new Student("Kim Joe", 20121024, 12, 32, 32)); linkedlist.addFirst(new Student("Hadi Ali", 20111025, 19, 38, 39)); linkedlist.addLast(new Student("Waleed Salim", 20131025, 10, 30, 30)); linkedlist.set(0, new Student("Khalid Ali", 20111027, 15, 30, 30)); linkedlist.removeFirst(); linkedlist.removeLast(); linkedlist.add(0, new Student("John Don",...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT