Question

Java: Given two sets, LinkedSet this and LinkedSet that, consisting of a generic type of linked...

Java: Given two sets, LinkedSet this and LinkedSet that, consisting of a generic type of linked lists, write the method that returns the union of "this" and "that". You can use the LinkedSet constructor to construct a new LinkedSet that represents the union of the two.

public Set<E> union(Set<E> that) { }

private LinkedSet(LinkedNode head) {
    this.head = head;
}

Homework Answers

Answer #1
public class LinkedSet<T> implements Set<T> {
        /*
         * inner class to represent node
         */
        private class LinkedNode {
                private T data;
                private LinkedNode next;

                public LinkedNode(T item) {
                        data = item;
                        next = null;
                }
        }

        private LinkedNode head;
        private int size;

        /*
         * adds item to bag if not already present
         * returns true if item successfully added, false if not
         */
        public boolean add(T item) {
                // to keep the last element
                LinkedNode prev = null;
                LinkedNode start = head;
                while (start != null) {
                        if (start.data.equals(item)) {
                                return false;
                        }
                        prev = start;
                        start = start.next;
                }

                if (head == null) {
                        head = new LinkedNode(item);
                } else {
                        prev.next = new LinkedNode(item);
                }
                size++;
                return true;
        }

        /*
         * removes item from set
         * returns removed item
         */
        public T remove() {
                if (size == 0) {
                        return null;
                }

                T removed = head.data;
                head = head.next;
                size--;
                return removed;
        }

        /*
         * returns item from set
         */
        public T get() {
                if (size == 0) {
                        return null;
                }

                return head.data;
        }

        /*
         * returns true if item is in set, false otherwise
         */
        public boolean contains(T item) {
                LinkedNode current = head;

                for (int i = 0; i < size; i++) {
                        if (current.data.equals(item)) {
                                return true;
                        }

                        current = current.next;
                }

                return false;
        }
        
        public Set<T> union(Set<T> that) { 
                LinkedSet<T> result = new LinkedSet<>();

                LinkedNode l1 = head;
                while(l1 != null) {
                        result.add(l1.data);
                        l1 = l1.next;
                }
                LinkedNode l2 = head;
                while(l2 != null) {
                        if(!result.contains(l2.data))
                                result.add(l2.data);
                        l2 = l2.next;
                }
                
                return result;
        }

        /*
         * returns size of set
         */
        public int size() {
                return size;
        }

        /*
         * returns string representation of contents of set
         */
        public String toString() {
                if (size == 0) {
                        return "[ ]";
                }

                String s = "[";

                LinkedNode current = head;

                while (current.next != null) {
                        s += current.data + ", ";

                        current = current.next;
                }

                return s + current.data + "]";
        }

        /*
         * checks if item is null and throws exception if so
         */
        private void checkForNull(T item) {
                if (item == null) {
                        throw new IllegalArgumentException("null not a possible value!");
                }
        }
}
**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Java Generic Linked List Problem: How do I remove a slice of a linked list and...
Java Generic Linked List Problem: How do I remove a slice of a linked list and add its data to another linked list in java? Example: Private<T> head; Private int size; Public List<T> slice(int from, int to) { // This method will remove the data from the given range (inclusive) and add it to a new List and return this new list. }
Given this definition of a generic Linked List node: public class LLNode {     private T...
Given this definition of a generic Linked List node: public class LLNode {     private T data;     private LLNode next;     public LLNode(T data, LLNode next) {           this.data = data;           this.next = next;     }     public void setNext(LLNode newNext){ next = newNext; }     public LLNode getNext(){ return next; }     public T getData() {return data;} } Write the findMinimumNode method body. This method returns the linked list node that contains the minimum value in the...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
) IN JAVA: Write a recursive method lowestPaidEmployeeRec that is placed in the linked list after...
) IN JAVA: Write a recursive method lowestPaidEmployeeRec that is placed in the linked list after the method countHighEarners. Method returns employee with lowest salary in entire linked lists of employees. Assume the same LinkedList class as is given as in question 10 above. // PRECONDITION: Linked list is not empty. public Employee lowestPaidEmployeeRec(Node first) // first is reference to the beginning of linked list. { }
Using the given SList as a template to implement a doubly linked list with the following...
Using the given SList as a template to implement a doubly linked list with the following functionalities Add first: add an element to the head of the list Add last: add an element to the end of the list Print: print the content of the list starting from the head Find: a private method that will return a reference to a node if it contains the same value of a given argument Insert after: a method that takes a key...
Java Program: You will be inserting values into a generic tree, then printing the values inorder,...
Java Program: You will be inserting values into a generic tree, then printing the values inorder, as well as printing the minimum and maximum values in the tree. Given main(), write the methods in the 'BSTree' class specified by the // TODO: sections. There are 5 TODOs in all to complete. Ex: If the input is like ferment bought tasty can making apples super improving juice wine -1 the output should be: Enter the words on separate lines to insert...
Language: JAVA(Netbeans) Write a generic class MyMathClass with at type parameter T where T is a...
Language: JAVA(Netbeans) Write a generic class MyMathClass with at type parameter T where T is a numeric object (Integer, Double or any class that extends java.lang.number) Add a method standardDeviation (stdev) that takes an ArrayList of type T and returns a standard deviation as type double. Use a for each loop where appropriate. Hard code a couple of test arrays into your Demo file. You must use at least 2 different types such as Double and Integer. Your call will...
The main goal is to implement two recursive methods, each is built to manipulate linked data...
The main goal is to implement two recursive methods, each is built to manipulate linked data structures. To host these methods you also have to define two utterly simplified node classes. 1.) Add a class named BinaryNode to the project. This class supports the linked representation of binary trees. However, for the BinaryNode class Generic implementation not needed, the nodes will store integer values The standard methods will not be needed in this exercise except the constructor 2.) Add a...
Implement Polynomial Linked List multiply method which multiply two polynomial and returns the product as a...
Implement Polynomial Linked List multiply method which multiply two polynomial and returns the product as a new polynomial. - It’s an instance method which called by a polynomial and takes second polynomial as the parameter pl. It should correctly multiply two polynomials together and returns this product polynomial. //please see the given code below public class PolynomialLinkedlist{ private static class PNode{ private int coe; private int exp; private PNode next; public PNode(int c, int e){ this(c, e, null); } public...
Write in Java (Not Javascript) Provide an implementation of priority queue using double-ended doubly linked lists....
Write in Java (Not Javascript) Provide an implementation of priority queue using double-ended doubly linked lists. Recall that double-ended means keeping first and last references and doubly linked feature allows us to go backwards, using a prev reference at each Link. Also, note that the greater the number, the lower the priority. For instance, 2 is of higher priority compared to 5. Specifically, write a class LinkedListPriorityQ which implements the priority queue methods: boolean isEmpty() void enqueue(int item) int dequeue()...