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

Please fill in the functions of the MyList class in java. Push adds a node to th

ID: 3708880 • Letter: P

Question

Please fill in the functions of the MyList class in java. Push adds a node to the front of the list and pop removes the node from the front of list. All other functions are self-explanatory. DO NOT ADD ANY NEW PRIVATE VARIABLES TO THE CLASS. MyNode and MyNodeIterator are defined below.

public class MyList<E> implements Iterable<E>, Cloneable, Comparable<MyList<E>> {

private MyNode<E> n;

protected int length;

// constructors

public MyList()

{

n = null;

length = 0;

}

public MyList(Iterable<E> iterable) {}

@Override

protected MyList<E> clone()

{

MyList<E> c = new MyList<E>(null, length);

for(E e : this)

{

}

return c;

}

@Override

public boolean equals(Object o) {}

@Override

public int hashCode() { }

@Override

public int compareTo(MyList<E> list) {return length - list.length; }

public Iterator<E> iterator() { }

public MyList<E> reverse() { }

public String toString() { }

public void push(E item) { }

public E pop() { }

public E peek() {return n.getVal(); }

public int getLength() {return length; }

}

///////////////////////////////////////////////MyNode.java/////////////////////////////////////////////////////////////////////

import java.lang.Iterable;

public final class MyNode<E> implements Iterable<E>{

// private fields

private final E val; // value field must be private

private MyNode<E> next; // next field must also be private

public MyNode (E val, MyNode<E> node) {

this.val = val;

next = node;

}

public MyNodeIterator<E> iterator() {return new MyNodeIterator<E>(this); }

public E getVal() { return val;}

public MyNode<E> getNext() { return next;}

public void setNext(MyNode<E> node) {next = node;}

}

/////////////////////////MyNodeIterator.java///////////////////////////////////////////////////

import java.util.Iterator;

import java.util.NoSuchElementException;

import java.lang.UnsupportedOperationException;

class MyNodeIterator<E> implements Iterator<E>{

private MyNode<E> p;

public MyNodeIterator (MyNode<E> n) {p = n;}

public boolean hasNext() { return p.getNext() != null;}

public E next() {

if (!hasNext()) throw new NoSuchElementException();

E res = p.getNext().getVal();

p.setNext(p.getNext().getNext());

return res;

}   

public void remove() { throw new UnsupportedOperationException(); }

}

Explanation / Answer

//method for adding a node at the front of the node

public void push(E item) { //passing the new item as parameter to function

//declaring a temp node and constructing a new node using the constructor ofMyNode class

MyNode<E> temp = new MyNode<E>(item, n);

//assigning the new node to n

n = temp;  

}

  

  

//method for deleting a node at the front of the node

public E pop() {

E e = n.getVal();

//declaring a temp node and constructing a new node using the constructor ofMyNode class

MyNode<E> temp = new MyNode<E>(n.getNext().getVal(), n.getNext().getNext());

//assigning the new node to n

n = temp;

return e;

}