Question

In: Computer Science

In Java, write the method public static void insertUnique(List l, T e), user of the ADT...

In Java, write the method public static void insertUnique(List l, T e), user of the ADT List. The method takes a list l and an element e and inserts the element at the end of the list only if it is not already there. Example 0.2. If l : A → B → C, then after calling insertUnique(l, "C"), the list does not change. Calling insertUnique(l, "D") will make l be : A → B → C → D.

Solutions

Expert Solution

import java.util.*;
public class linklist {

    public static void insertUnique(LinkedList<String> l,String e)
    {
        if(l.contains(e))
            System.out.println(l);
        else {
            l.add(e);
            System.out.println(l);
        }
    }
        public static void main(String args[])
        {
            // Creating object of the
            // class linked list
            LinkedList<String> l
                    = new LinkedList<String>();

            // Adding elements to the linked list
            l.add("A");
            l.add("B");
            l.add("C");
            System.out.println(l);

            insertUnique(l,"D");
            
        }
        
}

Explanation of Code

1. First we have imported the utils package which contain a in-build linked list class which we will be using to make the linked list.

import java.util.*;

2. Now in the public static void main() we have created an object of linkedlist class which is 'l'.

LinkedList<String> l = new LinkedList<String>();

3. Then we have used the add() which is available in the linkedlist class which allows us to insert values in the linked list. Then we have printed the list on othe output screen using println.

l.add("A");
l.add("B");
l.add("C");
System.out.println(l);

4. Then called the insertUnique() method .

insertUnique(l,"D");

5. Now let's see the insertUnique() method. In this method we have passed the linked list obejct 'l' and a String type object 'e' which is the value to be inserted in the list.

public static void insertUnique(LinkedList<String> l,String e)

6. Now in the body of insertUnique() we have checked if the String 'e' is already present in the list or not using the contains() method available in the linkedlist class. We have called the contains() method in if else block. If the contains() method returns true that means the linked list already contains the String 'e' so first if block will be executed and we have printed the list as it is.

  if(l.contains(e))
     System.out.println(l);

7. If the contains() method returns false then the else block will be executed. In the else block we have used another method available in the linkedlist class which is add(). add() adds a new value ot the linked list at the end. Then we have printed the new list.

  else {
     l.add(e);
     System.out.println(l);
  }

OUTPUT


Related Solutions

JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
(Sort ArrayList) Write the following method that sorts an ArrayList:   public static <E extends Comparable<E>> void...
(Sort ArrayList) Write the following method that sorts an ArrayList:   public static <E extends Comparable<E>> void sort(ArrayList<E> list) Write a program to test the method with three different arrays: Integers: 2, 4, and 3; Doubles: 3.4, 1.2, and -12.3; Strings: "Bob", "Alice", "Ted", and "Carol"
(Sort ArrayList) Write the following method that sorts an ArrayList: public static <E extends Comparable<E>> void...
(Sort ArrayList) Write the following method that sorts an ArrayList: public static <E extends Comparable<E>> void sort(ArrayList<E> list) Write a program to test the method with three different arrays: Integers: 2, 4, and 3; Doubles: 3.4, 1.2, and -12.3; Strings: "Bob", "Alice", "Ted", and "Carol" This program is similar to the Case Study in the book: Sorting an Array of Objects the language is in java
JAVA Given the header of a method public static void m1 (int[ ] max) Write down...
JAVA Given the header of a method public static void m1 (int[ ] max) Write down Java codes to invoke m1 method, declare variables as needed, (Do NOT implement the method)
Write a method public static <E> List<List<E>> filterMinLength(List<List<E>> listOfLists, int minLength) that given a list of...
Write a method public static <E> List<List<E>> filterMinLength(List<List<E>> listOfLists, int minLength) that given a list of lists and a minimum length returns a new list of lists, containing only the lists that are at least as long as the minimum length in the same order they appeared in the input. The original list must not be modified. For example, if listOfLists = [[1, 2, 3], [], [4, 5], [6, 7, 8, 9]] and minLength = 3, then the method should...
List.h template class List { // List class ADT              public:    virtual void...
List.h template class List { // List class ADT              public:    virtual void clear() = 0; // Inserts an item into the list at position index    virtual bool insert(const ListItemType& newItem) = 0;    // Append "it" at the end of the list    // The client must ensure that the list's capacity is not exceeded    virtual bool append(const ListItemType& newItem) = 0;    // Deletes an item from the list at a given position...
in java. using the following template as a method, public static void shellSort(int[] array) { create...
in java. using the following template as a method, public static void shellSort(int[] array) { create a program that counts the number of comparisons and swaps of the sorting method does using int array1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; // Best Case Test int array2[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; // Worst Case Test int array3[] = {10, 4, 8, 2, 6, 1, 9, 3, 7, 5}; //...
Re-write this method using iteration ONLY JAVA class Main { public static void merge(int arr[], int...
Re-write this method using iteration ONLY JAVA class Main { public static void merge(int arr[], int l, int m, int r) { int n1 = m - l + 1; int n2 = r-m; int left[] = new int[n1]; int right[] = new int[n2]; for (int i = 0; i < n1; ++i) left[i] = arr[l + i]; for (int j = 0; j < n2; ++j) right[j] = arr[m + 1 + j]; int i=0; //left int j=0; //...
Assume you call the method below with an array list that contains (2,3,4,5,1)   public static void...
Assume you call the method below with an array list that contains (2,3,4,5,1)   public static void m3(ArrayList list1) {        if (list1.size() ==1)   System.out.println(list1.get(0));        else               if (list1.size()%2==0)     System.out.println(list1.remove(0));               else                   System.out.println(list1.remove(list1.size()-1));             } What will be the output? Assume you call the method below with an array list that contains (2,3,4,5,1)   public static void m3(ArrayList list1) {        if (list1.size() ==1)   System.out.println(list1.get(0));        else               if (list1.size()%2==0)     System.out.println(list1.remove(0));               else                   System.out.println(list1.remove(list1.size()-1));             } What will be the output?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT