I have to implement a method:
E[] toArray(E[] a) // Pass an array, convert to singly linked list, then return the array.
from java.util
Interface List<E>
As I mentioned, I have to pass an array, convert it to a singly linked list, sort it, then return the array.
In the Node
class I have this to work with:
public Node(E v, Node<E> next) {
// pre: v is a value, next is a reference to remainder of list
// post: an element is constructed as the new head of list
data = v;
nextElement = next;
}
public Node(E v) {
// post: constructs a new tail of a list with value v
this(v,null);
}
public Node<E> next() {
// post: returns reference to next value in list
return nextElement;
}
public void setNext(Node<E> next) {
// post: sets reference to new next value
nextElement = next;
}
public E value() {
// post: returns value associated with this element
return data;
}
public void setValue(E value) {
// post: sets value associated with this element
data = value;
}
Am I barking up the wrong tree or can someone help me with this here? Sorry if this is the wrong place for such questions.