Question

In: Computer Science

Please implement the java method addInOrder() that allows you to create and maintain the lists in...

Please implement the java method addInOrder() that allows you to create and maintain the lists in the order required. The addInOrder method must work with different external Comparator objects. (Hint: Consider creating and using a private compare method and a private Comparator reference as members of your SLL class. If your SLL is constructed without any parameter, then you should initialize the internal Comparator object reference to null. Otherwise, you should initialize it to the external Comparator object passed as a parameter to the constructor. The private compare method should then make the comparison based on the external Comparator if it exists, and based on the data item’s internal Comparable implementation otherwise.)

I have tried to implement this in my below code for the SLL java class and think that everything besides the addInOrder method meets the above requirements but the addInOrder needs to be corrected.

Here is the code for the Node java class I was using for the Singly Linked List:

public class Node<T extends Comparable<T>> {

   private T data;
   private Node next;

   /**
   * Constructor for objects of class Node
   */
   public Node(T d) {
       data = d;
       next = null;
   }

   public T getData() {
       return data;
   }

   public void setData(T o) {
       data = o;
   }

   public Node getNext() {
       return next;
   }

   public void setNext(Node n) {
       next = n;
   }

   @Override
   public String toString() {
       return "Node [data=" + data + ", next=" + next + "]";
   }  

  

}

Here is the current code for my SinglyLinked java class that needs the addInOrder method to be implemented:

import java.util.Comparator;

//Generic type that restrict type to objects that have implemented the comparable interface.
public class SLL<T extends Comparable<T>> {
   private Node <T> head;
   private Node <T> tail;

   private int size;

   private Comparator <T> comparator;

   public SLL() {
       head = null;
       size = 0;

       comparator = null;
   }

   //External comparator
   public SLL(Comparator <T> externalComp) {

       head = null;
       comparator = externalComp;
       size = 0;

   }

   //Method that gives size of the list
  
   public int size() {
       for(Node <T> n = head; n.getNext() != null; n = n.getNext())
       size++;   
       return size;
       }


   //this method will be used with the add in order method
   private int compare(T obj1, T obj2) {
       if(comparator == null) {
           return obj1.compareTo(obj2);
       }

       else {
           return comparator.compare(obj1, obj2);
       }
   }

   //addInOrder, add, delete, find, etc and once these methods are finished we can work with the A2 SLL and implement the rest of the assignment with   
   private void addHead(Node <T> n) {
       if (head == null) {
           head = n;
           tail = n;
       } else {
           n.setNext(head);
           head = n;
       }

   }

   private void addTail(Node<T> n) {
       if (tail == null) { // list is empty
           head = n;
           tail = n;
       } else {
           tail.setNext(n);
           tail = n;
       }
   }

   private Node<T> findItem(T key) {
       Node <T> currentNode = head;
       while (currentNode != null) {
           /**
           * Visit the node. In this case,
           * if the data in the currentNode is equal to the key,
           * return the reference to the node.
           */

           if (currentNode.getData().equals(key))
               return currentNode;
           // else, move to the next node
           else
               currentNode = currentNode.getNext();
       }
       // Return null if it didn't find the node.
       return null;
   }

   private Node<T> deleteItem(T key) {
       Node <T> currentNode = head;
       Node <T> previousNode = head;
       while (currentNode != null) {
           // Visit the node. In this case,
           // if the data in the currentNode is equal to the key,
           // remove the current node from the list by updating the reference
           // of the previous node to point to the node after the currentNode.
           if (currentNode.getData().equals(key)) {
               if (head == tail) {
                   head = null;
                   tail = null;
                   return currentNode;
               }

               if (currentNode == head)
                   head = currentNode.getNext();
               else {
                   previousNode.setNext(currentNode.getNext());
               }
               if (currentNode == tail) {
                   tail = previousNode;
               }

               return currentNode;
           }
           // else, move to the next node
           else {
               previousNode = currentNode;
               currentNode = currentNode.getNext();
           }
       }
       // Return null if it didn't find the node.
       return null;
   }
  

   private void addInOrder(Node <T> n) {
      
       if(head == null || compare(n,head) <= 0){
          
           addHead(n);

       }

       else if(compare(n, tail) > 0) {
           addTail(n);
       }

       else{
           Node mover = head;

           while(mover.getNext() != null && compare(n, mover.getNext()) > 0)
           {
               mover = mover.getNext();
           }

           n.setNext(mover.getNext());
           mover.setNext(n);
       }
      
   }


}

Solutions

Expert Solution

// Node.java
public class Node<T extends Comparable<T>> {

private T data;
private Node next;

/**
* Constructor for objects of class Node
*/
public Node(T d) {
data = d;
next = null;
}

public T getData() {
return data;
}

public void setData(T o) {
data = o;
}

public Node getNext() {
return next;
}

public void setNext(Node n) {
next = n;
}

@Override
public String toString() {
return "Node [data=" + data + ", next=" + next + "]";
}

  

}

// end of Node.java


// SLL.java

import java.util.Comparator;

//Generic type that restrict type to objects that have implemented the comparable interface.
public class SLL<T extends Comparable<T>> {
private Node <T> head;
private Node <T> tail;

private int size;

private Comparator <T> comparator;

public SLL() {
head = null;
size = 0;

comparator = null;
}

//External comparator
public SLL(Comparator <T> externalComp) {

head = null;
comparator = externalComp;
size = 0;

}

//Method that gives size of the list
  
public int size() {
for(Node <T> n = head; n.getNext() != null; n = n.getNext())
size++;   
return size;
}


//this method will be used with the add in order method
private int compare(T obj1, T obj2) {
if(comparator == null) {
return obj1.compareTo(obj2);
}

else {
return comparator.compare(obj1, obj2);
}
}

//addInOrder, add, delete, find, etc and once these methods are finished we can work with the A2 SLL and implement the rest of the assignment with   
private void addHead(Node <T> n) {
if (head == null) {
head = n;
tail = n;
} else {
n.setNext(head);
head = n;
}

}

private void addTail(Node<T> n) {
if (tail == null) { // list is empty
head = n;
tail = n;
} else {
tail.setNext(n);
tail = n;
}
}

private Node<T> findItem(T key) {
Node <T> currentNode = head;
while (currentNode != null) {
/**
* Visit the node. In this case,
* if the data in the currentNode is equal to the key,
* return the reference to the node.
*/

if (currentNode.getData().equals(key))
return currentNode;
// else, move to the next node
else
currentNode = currentNode.getNext();
}
// Return null if it didn't find the node.
return null;
}

private Node<T> deleteItem(T key) {
Node <T> currentNode = head;
Node <T> previousNode = head;
while (currentNode != null) {
// Visit the node. In this case,
// if the data in the currentNode is equal to the key,
// remove the current node from the list by updating the reference
// of the previous node to point to the node after the currentNode.
if (currentNode.getData().equals(key)) {
if (head == tail) {
head = null;
tail = null;
return currentNode;
}

if (currentNode == head)
head = currentNode.getNext();
else {
previousNode.setNext(currentNode.getNext());
}
if (currentNode == tail) {
tail = previousNode;
}

return currentNode;
}
// else, move to the next node
else {
previousNode = currentNode;
currentNode = currentNode.getNext();
}
}
// Return null if it didn't find the node.
return null;
}
  

private void addInOrder(Node <T> n) {
  
       // pass the value stored in the node, not the node objects to compare method
       if(head == null || compare(n.getData(),head.getData()) <= 0){
  
addHead(n);

       }

       // pass the value stored in the node, not the node objects to compare method
       else if(compare(n.getData(), tail.getData()) > 0) {
addTail(n);
       }

else{

       Node mover = head;

while(mover.getNext() != null && compare(n.getData(), mover.getNext().getData()) > 0)
{
mover = mover.getNext();
}

n.setNext(mover.getNext());
mover.setNext(n);
       }
  
}


}

//end of SLL.java


Related Solutions

Java: Create a class and name it MyArray and implement following method. * NOTE: if you...
Java: Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items in the...
Java - Design and implement an application that creates a histogram that allows you to visually...
Java - Design and implement an application that creates a histogram that allows you to visually inspect the frequency distribution of a set of values. The program should read in an arbitrary number of integers that are in the range 1 to 100 inclusive; then produce a chart similar to the one below that indicates how many input values fell in the range 1 to 10, 11 to 20, and so on. Print one asterisk for each value entered. Sample...
Please write a Java algorithm solving the following problem: Implement a Java method to check if...
Please write a Java algorithm solving the following problem: Implement a Java method to check if a binary tree is balanced. For this assignment, a balanced tree is defined to be a tree such that the heights of the two subtrees of any node never differ by more than one. 1. First, please create the following two classes supporting the Binary Tree Node and the Binary Tree: public class BinTreeNode<T> { private T key; private Object satelliteData; private BinTreeNode<T> parent;...
Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
Android Studio. Java. Please create an application that -> An activity that allows user to enter...
Android Studio. Java. Please create an application that -> An activity that allows user to enter name, gender, date of birth, state of residence (selected from a pre-defined list: CA, AZ, NV, OR), email address and favorite website. This activity has a button "Show Data" that displays detail entered
java please        * implement zip method in Q1 class to merge two linkedlists into...
java please        * implement zip method in Q1 class to merge two linkedlists into one.        * The elements in the result are made by concatenating elements in different        * linkedlists one after one. The order of the elements matters.        * For example, merge(list1, list2) should return [1,5,2,4,3,3,4,2,5,1].        * You can assume that arr1 and arr2 has the same size.        * HINT: You should use ListIterator to make it...
you will create a program with Java to implement a simplified version of RSA cryptosystems. To...
you will create a program with Java to implement a simplified version of RSA cryptosystems. To complete this project, you may follow the steps listed below (demonstrated in Java code) to guide yourself through the difficulties. Step I Key-gen: distinguish a prime number (20 pts) The generation of RSA's public/private keys depends on finding two large prime numbers, thus our program should be able to tell if a given number is a prime number or not. For simplicity, we define...
In java please create a method that will insert a value after a specific node for...
In java please create a method that will insert a value after a specific node for a linked list. public void insertAfter(Node a, int newData){ }
Please do this in java program. In this assignment you are required to implement the Producer...
Please do this in java program. In this assignment you are required to implement the Producer Consumer Problem . Assume that there is only one Producer and there is only one Consumer. 1. The problem you will be solving is the bounded-buffer producer-consumer problem. You are required to implement this assignment in Java This buffer can hold a fixed number of items. This buffer needs to be a first-in first-out (FIFO) buffer. You should implement this as a Circular Buffer...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT