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

package com.list; public class Main { public static void main(String[] args) { N

ID: 3675575 • Letter: P

Question


package com.list;

public class Main {

   public static void main(String[] args) {
      
       NodeList list = new NodeList();
      
       list.add("Book");
       list.add("Lappy");
       System.out.println("Length : "+list.size());

       list.remove("Book");
       System.out.println("Length : "+list.size());
              
       list.remove("Lappy");
       System.out.println("Length : "+list.size());
              
                list.add("Book");
       list.add("Lappy");
                list.add("Glass");
                System.out.println("Length : "+list.size());
              
                list.iterate();
   }
}


package com.list;


public class Node {

   private Object data;
   private Node next;
  
   public Node(Object data) {
            this.data = data;
            this.next = null;
   }
      
        public Node(Object data, Node next){
            this.data = data;
            this.next = next;
        }

   /**
   * @return data
   */
   public Object getData() {
       return data;
   }

   /**
         * @param data :the data to set
         */
   public void setData(Object data) {
       this.data = data;
   }

   /**
   * @return String of the data
   */
   public String toString() {
       return "Data: "+ data.toString();
   }

        /**
         * @return the next
         */
        public Node getNext() {
            return next;
        }

        /**
         * @param next :the next to set
         */
        public void setNext(Node next) {
            this.next = next;
        }
}


package com.list;

public class NodeList {
    private int size = 0;
    private Node root = null;
  
    /*
     * It has to take a new Node and add that to the next address of previous Node.
     *
     * @Param - Node
     */
    public void add(Object data) {
            if (data==null) return;
            if (root == null) {
                Node newNode = new Node(data, null);
                root = newNode;
            }
            else {
                Node currentNode = root;
                while(currentNode.getNext()!=null){
                    currentNode = currentNode.getNext();
                }
                Node newNode = new Node(data, null);
                currentNode.setNext(newNode);
            }
            size++;
    }

    /*
     * It has to return the size of the NodeList
     *
     * @return size
     */
    public int size() {
        return size;
    }

    /*
     * It has to take a Node and remove that node if you find it in the list
     * from the existing nodes otherwise dont do anything.
     *
     * @Param - Node
     */

    public void remove(Object data) {
        if(data==null) return;
        if(root != null) {
            if(root.getData() == data) {
                if(root.getNext()==null)
                    root = null;
                else
                {
                    root = root.getNext();
                }
              
            } else {
                Node currentNode = root;
                while(currentNode.getNext()!= null){
                   Node temp = currentNode.getNext();
                   if(temp.getData() == data)
                   {
                       Node tempsNext = temp.getNext();
                       currentNode.setNext(tempsNext);
                       temp = null;
                   }
                }
            }
   size--;
        }
    }
    /**
     * Start with the head and traverse till you reach null.
     */
    public void iterate(){
  
    }
}

Explanation / Answer

Please find the required iterate function below :