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

Problem 2. (50 Points) Implement a nicer linked list. You may notice in Problem

ID: 3707386 • Letter: P

Question

Problem 2. (50 Points) Implement a nicer linked list. You may notice in Problem 1 that it is rather inconvenient to build lists with MyNode. Implement another generic class MyList, in terms of MyNode, that has a nicer interface. Class MyList must have a private field MyNode n and a protected field int length. Task 1. (5 points) Make MyList iterable by implementing the Iterable interface and giving an implementation of iterator() Task 2. (5 points) Make MyList clonable by implementing the Cloneable interface and overriding the Object.clone) method. You must give your own explicit implementation for the clone () method using the for-each loop. Task 3. (5 points) Make MyList comparable by implementing the Comparable

Explanation / Answer

class MyNodeIterator<E> implements Iterator<E>

{

    private MyNode<E> p;

   

    // constructor

    MyNodeIterator(MyNode<E> n)

    {

        this.p = n;

    }

   

    // return true if the current node is not null

    public boolean hasNext()

    {

        return this.p != null;

    }

   

    public E next()

    {

        // get the value of current node

        E ans = this.p.getVal();

       

        // move to the next node

        this.p = this.p.getNext();

    }

   

}

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