Question

I am trying to check for duplicates in a private method I created. I don't think...

I am trying to check for duplicates in a private method I created. I don't think I am implementing it correctly. I was hoping to get some help. Below are the methods I need to check for duplicates and also the private Boolean method that checks for duplicates.

public LinkedList2<T> union( LinkedList2<T> other )
   {
       LinkedList2 unionSet = new LinkedList2() ;
       LinkedList2 unionSet2 = other;
       Node curr;
       for(curr = head; curr != null; curr = curr.getNext())
       {
           unionSet.insertAtTail(curr.getData());
  
       }
       for(curr = unionSet2.head; curr != null; curr = curr.getNext())
       {
           if(!unionSet.contains(curr.getData())&& checkingForDupes(unionSet)== false)
           {
               unionSet.insertInOrder(curr.getData());
          
           }
       }
       return unionSet;
      
   }
   public LinkedList2<T> inter( LinkedList2<T> other )
   {
       LinkedList2 interSet = new LinkedList2<T> () ;
       Node<T> interSet2 = this.head;
       Node<T> interSet3 = other.head;
      
       while(interSet2.getNext()!=null)
       {
           interSet3 = other.head;
           while(interSet3 != null)
           {
               if(interSet2.getData().equals(interSet3.getData()) && checkingForDupes(interSet)== false)
               {
                   interSet.insertInOrder(interSet3.getData());
               }

               interSet3 = interSet3.getNext();
           }
           interSet2 = interSet2.getNext();
       }
       return interSet;
      
   }
   public LinkedList2<T> diff( LinkedList2<T> other )
   {
       LinkedList2 diffList = new LinkedList2 ();
       Node<T> diff2 = this.head;
       Node<T> diff3 = other.head;
       while(diff2 != null)
       {
           diff3 = other.head;
           while(diff3 != null)
           {
               if(diff2.getData().equals(diff3.getData()) && checkingForDupes(diffList)== false)
               {
                   diff2 = diff2.getNext();
               }
               diff3 = diff3.getNext();
           }
           diffList.insertInOrder(diff2.getData());
           diff2 = diff2.getNext();
       }
       return diffList;
   }
   private boolean checkingForDupes(LinkedList2<T> other)
   {
       int currentIndex = 0;
       for (Node<T> curr = other.head; curr != null; curr = curr.getNext())
       {
           int nodeIndex = 0;
           for (Node<T>thing = other.head; thing!= null; thing = thing.getNext())
           {
               if (currentIndex != nodeIndex && thing.equals(curr))
               {
                   return true;
               }
               nodeIndex++;
           }
           currentIndex++;
       }
       return false;
   }

Homework Answers

Answer #1

In the given code the code to check for duplicates is little bit redundant as in both the loops starts from the other.head which is not required.The code is provided below:

private boolean checkingForDupes(LinkedList2<T> other)
{
Node<T> curNode = other.head;

while (curNode)
{
Node<T> tempNode = curNode.getNext();

while (tempNode)
{
if (tempNode.equals(curNode))
{
return true;
}

tempNode = tempNode.getNext();
}

curNode = curNode.getNext();
}
return false;
  }

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
When I run the main method with the test case, it returns the list backwards. How...
When I run the main method with the test case, it returns the list backwards. How can I fix it so that it gives me all 6 correct actions? My guess is that the problem is in the getAction() function. public class StringDoublyLinkedList { /** * A private class to represent a link in the linked list. */ private class Node { String value; Node next; Node prev; Node(String value) { this.value = value; this.next = null; this.prev = null;...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using pointers correctly and deleting memory properly? #ifndef DYNAMICARRAY_H #define DYNAMICARRAY_H #include <cstdlib> #include <iostream> using namespace std; // Node class class Node { int data; Node* next; Node* prev; public: Node(); Node(int); void SetData(int newData) { data = newData; }; void SetNext(Node* newNext) { next = newNext; }; void SetPrev(Node* newPrev) { prev = newPrev; }; int getData() { return data; }; Node* getNext()...
This is the java code that I have, but i cannot get the output that I...
This is the java code that I have, but i cannot get the output that I want out of it. i want my output to print the full String Form i stead of just the first letter, and and also print what character is at the specific index instead of leaving it empty. and at the end for Replaced String i want to print both string form one and two with the replaced letters instead if just printing the first...
How do I implement this method BalancedByNodeCount() ? public class BinarySearchTree { private Node root; private...
How do I implement this method BalancedByNodeCount() ? public class BinarySearchTree { private Node root; private boolean isBalancedByNodeCount() { /**************************************************************************** Implement this method and replace the return statement below with your code. * Definition of Balance tree (On page 372 of book): * An unbalanced tree is created when most of the nodes are on one side of the root or the other. ****************************************************************************/    return false; }       public static void main(String args[]) { int[] values1 = {50,...
Task 1: You will modify the add method in the LinkedBag class.Add a second parameter to...
Task 1: You will modify the add method in the LinkedBag class.Add a second parameter to the method header that will be a boolean variable: public boolean add(T newEntry, boolean sorted) The modification to the add method will makeit possible toadd new entriesto the beginning of the list, as it does now, but also to add new entries in sorted order. The sorted parameter if set to false will result in the existing functionality being executed (it will add the...
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...
(Please use Java Eclipse) QUESTION 1: For the question below, assume the following implementation of LinkedQueue:...
(Please use Java Eclipse) QUESTION 1: For the question below, assume the following implementation of LinkedQueue: public static final class LinkedQueue<T> implements QueueInterface<T> {     private Node<T> firstNode;     private Node<T> lastNode;     public LinkedQueue() {         firstNode = null;         lastNode = null;     }     @Override     public T getFront() {         if (isEmpty()) {             return null;         }         return firstNode.getData();     }     @Override     public boolean isEmpty() {         return firstNode == null;    ...
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...
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...
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue...
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue class . Call your class Queue. it must be a template class. public class Queue { } I have put a driver program in the module . It is called CircularQueue.java This driver program should then run with your Queue class (no modifications allowed to the driver program). Your Queue class should have at least the following methods: one or more constructors, enqueue, dequeue,...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT