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

HERE IS MY LINKLIST CODE I HAVE WROTE, I have three major problem method which a

ID: 3538776 • Letter: H

Question

HERE IS MY LINKLIST CODE I HAVE WROTE, I have three major problem method which are add(pos,data),remove and toString are costing me error, also sometime I get this message "java.lang.NullPointerException",please help me fix or you can write a new one.


public class SinglyLinkedList<E> implements ILinkedList<E>{

    /** Node to keep track of the head (beginning of the list) */
    private SinglyNode<E> head;
    /** Logical size of the list */
    private int size;
   
   
    /** initializes*/
    public SinglyLinkedList(){
       
        head = null;
        size = 0;
    }
   
    /** Adds the parameter to the beginning of the list*/
    public void add(E data) {
   
        if (size == 0){
           
            //add into beginning of the list
            SinglyNode<E> cu = this.head;
            this.head = new SinglyNode<E>(data);
           
        }else{
           
            SinglyNode<E> cu = head;
            SinglyNode<E> current = null;
            while((current = cu.next) != null) //find the last Node
                cu = cu.next;
            cu.next = new SinglyNode<E>(data);

           
        }
       
            size++;
    }

    /* problem method: can not add in particular position*/
   
    public void add(int pos, E data) throws Exception {

        if(data == null){                                //check if item is null
           
            throw new NullPointerException();

        }else if(pos < 0 || pos > size) {               //check if the index is small than 1 or not
                                                        //check if the index is exist or not    
            throw new Exception("No such index");
           
        }else if (pos == 0) {                          
           
            head = new SinglyNode<E>(data);
           
        }else{
           
            SinglyNode<E> current = head;                //Temporary Node
           
            for (int i = 0; i < pos - 1; i++) {
               
                current = current.next;
            }
                current.next = new SinglyNode<E> (data);
        }
            size++;
       
    }

    /**Returns true if the list contains the specified element. */
    public boolean contains(E data) {
           
            try {
               
                return indexOf(data) != -1;   //return data of index
               
            } catch (Exception e) {

                e.printStackTrace();
            }
            return false;
       
    }

    /**Gets the element at the specified position*/
    public E get(int pos) throws Exception {

       
        if (pos < 0 || pos >= size) {                //check if the index is out of bounds or not
            throw new Exception("out of bounds");
        }   
       
        SinglyNode<E> temp = head;               
       
        for (int i = 0; i < pos; i++) {                //loop through the list to find the index and return the index of data
            temp = temp.next;
        }
       
        return temp.data;
       
    }
   
    /**
    * Finds the index of the first occurrence of the specified element.
    * Note that it throws an Exception if the element is not found in the list
    * The exception message is "not found"*/
    public int indexOf(E data) throws Exception {
                                               
        SinglyNode<E> temp = head;     //current position

        int i = 0;                        //index
       
        for(i = 0; !(temp.data).equals(data) && temp != null; i++){    //try to find the data we are looking for
           
            if(i == size()) {
                throw new Exception("not found");            //check if not found
            }
           
            temp = temp.next;                       
        }
        return i;

    }

    /**Returns true if list is empty*/
    public boolean isEmpty() {
       
        if(head == null){
           
            return true;
           
        }else
           
            return false;
    }


    /*Problem: I can remove the index after 0 but not index 0 it will cost error*/


    public void remove(E data) throws Exception {
       
       
        SinglyNode<E> current = head;
        SinglyNode<E> temp = null;    //set node empty for later update
       
        if(head.data.equals(data)){

            head = head.next;
            size--;

        }
       
        while(current != null && !current.data.equals(data)){
       
            temp = current;
            current = current.next;
           
        }
       
        if(current == null) throw new Exception("not found");
       
            temp.next = current.next;
            current = null;
       
        size--;

    }

    /**
    * Replaces the element at the specified position with the provided data
    * Note that it throws an Exception if the position is non-existent
    * (for example, if you want to insert at location 3 in an empty list).
    * Note that the first element is considered to be at index position 0.
    * The exception message is "out of bounds"*/
    public void set(int pos, E data) throws Exception {
       
        if(data == null){                                //check if item is null
           
            throw new NullPointerException();

        }else if(pos < 0 || pos > size) {               //check if the index is small than 1 or not
                                                        //check if the index is exist or not    
            throw new Exception("out of bounds");
           
        }else{
               
            SinglyNode<E> current = head;           //get a reference to the node object representing the index element
            for (int i = 0; i < pos; i++) {
                current = current.next;
            }
            current.data = data;
        }
    }

    /** Size of the list */
    public int size() {
       
        return size;
    }
    /** print list as string */
    public String toString() {

            SinglyNode<E> current = head;
            StringBuffer print = new StringBuffer();   //create the container for store the string
           
            if(isEmpty()){                            //print empty message if the list is empty
                System.out.print("<EMPTY>");
            }
            while (current != null) {                //loop through the list if the current node is not null
               
                print.append (current.data.toString()); //copy the data into string
                current = current.next;
             
              if(current != null) {                   
               
                print.append (" , ");
               
              }
             
            }
                return print.toString();
    }

}

Explanation / Answer

need more time