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 ComparableExplanation / 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();
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.