Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please complete the code public class SinglyLinkedList t private static class No

ID: 3858740 • Letter: P

Question

Please complete the code

public class SinglyLinkedList t private static class Node private E value; private Node next; private Node(E value,Node next) this. value value; this.next next; // Instance variable private Node head; // SinglyLinkedList methods / returns true if the list is empty*/ public boolean isEmpty) return head null; public void addfirst(E item) t //this method will be use to populate the list if (item = null) { throw new NullPointerException("Illegal argument"); head new Node(item, head); //The recursive method size returns the number of nodes in the list public int size)t // the public size method calls a private recursive method size starting from head return size(head); private int size(Node p)t //complete this method / /The recursive method get returns the element at position index (first element is at position 0) public E get( int index ) if (index0) return null; else return .....i// make a call to the private recursive method

Explanation / Answer

Given below is the completed code for the question. I have a small suggestion for you. Please copy paste the text of the code , rather than screen shot. It will be easy to start coding right away and debug. In this question, since it was image, I had to type all of the code again in order to test if the implementation was correct.

I have implementted all the methods where "complete this method" is written.

Since your original file has comments which I have not typed in my code, I suggest you copy the method implementation one by one from this answer into your original code. Replace the code where ever //complete this method is written, with the one from the answer. Most of the recursive functions have a public and private version. So carefully copy it over. You could have avoided this copy paste work if you had pasted the code as a text rather than the image. After your have copied over the implementations for missing code, you can uncomment the commented lines in main () to get the output.

Please don't forget to rate the answer if it helped. Thank you.

public class SinglyLinkedList <E extends Comparable<E>> {

   private static class Node<E>{

       private E value;

       private Node<E> next;

      

       private Node(E value, Node<E> next){

           this.value = value;

           this.next = next;

       }

   }

  

   private Node<E> head;

  

   public boolean isEmpty(){

       return head == null;

   }

  

   public void addFirst(E item){

       if(item == null){

           throw new NullPointerException("Illegal argument");

       }

      

       head = new Node<E>(item, head);

   }

  

   /******************** size *******/

   public int size(){

       return size(head);

   }

   private int size(Node<E> p){

       if(p == null)

           return 0;

       else

           return 1 + size(p.next);

   }

  

   /******************** get *********/

   public E get(int index){

       if(index < 0)

           return null;

       else

           return get(head, index);

   }

  

   private E get(Node<E> p, int index){

       if(p == null)

           return null;

       else{

           if(index == 0)

               return p.value;

           else

               return get(p.next, index-1);

       }

   }

  

   /******************** contains *********/

   public boolean contains(E value){

       return contains(head, value);

   }

   private boolean contains(Node<E> p, E value){

       if(p == null )

           return false;

       else {

           if(p.value == value)

               return true;

           else

               return contains(p.next, value);

       }

   }

  

  

   /******************** countGreaterThan ********/

   public int countGreaterThan(E threshold){

       return countGreaterThan(threshold, head);

   }

  

   private int countGreaterThan(E threshold, Node<E> node){

       if(node == null)

           return 0;

       else{

           int count = countGreaterThan(threshold, node.next);

           if(node.value.compareTo(threshold) > 0)

               count ++;

           return count;

       }

   }

  

  

   /******************** findMax *********/

   public E findMax(){

       if(head == null){

           return null;

       }

       else{

           return findMax(head);

       }

   }

  

   private E findMax(Node<E> p){

       if(p == null)

           return null;

       else{

           E max = findMax(p.next);

           if(max == null)

               return p.value;

           else

           {

               if(p.value.compareTo(max) > 0)

                   return p.value;

               else

                   return max;

           }      

       }

          

   }

  

  

   /******************** remove ********/

   public void remove(E o){

       if(head != null){

           if(head.value.compareTo(o) == 0){

               head = head.next;

               Node<E> temp = head;

               temp.value = null;

               temp.next = null;

           }

           else remove(head, o);

       }

   }

  

   private void remove(Node<E> p, E o){

       if(p.next == null){

           return;

       }

       if(p.next.value.compareTo(o) == 0){

           Node<E> temp = p.next;

           p.next = p.next.next;

           temp.value = null;

           temp.next = null;

           return;

       }

       else{

           remove(p.next, o);

       }

      

   }

  

   /******************** duplicate *****/

   public SinglyLinkedList<E> duplicate(E item){

       return duplicate(item, head);

   }

   private SinglyLinkedList<E> duplicate(E item, Node<E> node){

       if(node == null)

           return new SinglyLinkedList<E>();

       else{

           SinglyLinkedList<E> result = duplicate(item, node.next);

           result.addFirst(node.value);

           if(node.value.compareTo(item) == 0)

           {

               result.addFirst(item);

           }

           return result;

          

       }

   }

  

  

   /******************** storeGreaterThan ******/

   public SinglyLinkedList<E> storeGreaterThan(E threshold){

       return storeGreaterThan(threshold, head);

   }

  

   private SinglyLinkedList<E> storeGreaterThan(E threshold, Node<E> node){

       if(node == null)

           return new SinglyLinkedList<E>();

       else{

           SinglyLinkedList<E> result = storeGreaterThan(threshold, node.next);

           if(node.value.compareTo(threshold) > 0)

               result.addFirst(node.value);

           return result;

          

       }

      

   }

  

   public boolean equals(Object obj){

       if(!(obj instanceof SinglyLinkedList<?>)){

           return false;

       }

       if(obj == this){

           return true;

       }

       else

           return equals((SinglyLinkedList<?>) obj);

   }

  

   public boolean equals(SinglyLinkedList<E> other){

       return equals(this.head, other.head);

   }

  

   private boolean equals(Node<E> p, Node<E> q){

       if(p == null){

           if(q == null){

               return true;

           }

           else{

               return false;

           }

       }

       else{

           if(p.value.compareTo(q.value) == 0)

               return equals(p.next, q.next);

           else

               return false;

       }

   }

  

  

   public String toString(){

       return "{" + (mytoString(head)) + "}";

    }

  

   private String mytoString(Node<E> p ){

       String result ="";

       if(p != null){

           if(p.next != null){

               result = p.value + "," + mytoString(p.next);

           }

           else{

               result = "" + p.value;

           }

       }

      

       return result;

   }

  

  

   public static void main(String[] args) {

       SinglyLinkedList<Integer> mylist = new SinglyLinkedList<Integer>();

       SinglyLinkedList<Integer> yourlist = new SinglyLinkedList<Integer>();

       System.out.println(mylist);

       mylist.addFirst(15);

       mylist.addFirst(17);

       mylist.addFirst(3);

       mylist.addFirst(22);

       mylist.addFirst(90);

       mylist.addFirst(12);

       mylist.addFirst(38);

       mylist.addFirst(25);

       System.out.println("Here is my list: " + mylist);

       System.out.println("the size of my list is : " + mylist.size());

       System.out.println("if you are wondering if hte list contains 22 the answer is " + mylist.contains(22));

       System.out.println("if you are wondering if hte list contains 23 the answer is " + mylist.contains(23));

       System.out.println("if you are wondering about the element at postion 4, it is " + mylist.get(4));

       System.out.println("if you are wondering about the element at postion 0, it is " + mylist.get(0));

      

       System.out.println("the number of elements larger than 10 is " + mylist.countGreaterThan(10));

       System.out.println("the number of elements larger than 30 is " + mylist.countGreaterThan(30));

       System.out.println("the largest element is " + mylist.findMax());

       yourlist = mylist.duplicate(90);

       System.out.println("here is your new list with 90 duplicated: " + yourlist);

       System.out.println("if you are wondering if your list is similar to mine, the answer is: " + mylist.equals(yourlist));

       System.out.println("if you are wondering if my list is similar to your, the answer is: " + yourlist.equals(mylist));

       yourlist.remove(90);

       System.out.println("here is your new list with 90 removed: " + yourlist);

       System.out.println("if you are wondering if your list is similar to mine, the answer is: " + mylist.equals(yourlist));

       System.out.println("if you are wondering if my list is similar to your, the answer is: " + yourlist.equals(mylist));

       yourlist = mylist.storeGreaterThan(20);

       System.out.println("here is your new list with only numbers that are larger than 20: " + yourlist);

      

   }

}

output

{}

Here is my list: {25,38,12,90,22,3,17,15}

the size of my list is : 8

if you are wondering if hte list contains 22 the answer is true

if you are wondering if hte list contains 23 the answer is false

if you are wondering about the element at postion 4, it is 22

if you are wondering about the element at postion 0, it is 25

the number of elements larger than 10 is 7

the number of elements larger than 30 is 2

the largest element is 90

here is your new list with 90 duplicated: {25,38,12,90,90,22,3,17,15}

if you are wondering if your list is similar to mine, the answer is: false

if you are wondering if my list is similar to your, the answer is: false

here is your new list with 90 removed: {25,38,12,90,22,3,17,15}

if you are wondering if your list is similar to mine, the answer is: true

if you are wondering if my list is similar to your, the answer is: true

here is your new list with only numbers that are larger than 20: {25,38,90,22}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote