created a linkedlist class,the set() method and listiterator() method. I need someone to finish for me.
here is what i got so far:
import java.util.Iterator;
import java.util.ListIterator;
import java.util.NoSuchElementException;
public class MyLinkedList {
private Node head = null;
private Node tail = null;
private Node temp = null;
private int counter = 0;
public MyLinkedList() {
}
public int size(){return counter;}
public Product get(int position){
assert (position >= 0 && position < size());
temp = head;
for(int i = 0; i < position; i++) temp = temp.next;
return temp.element;
}
public void add(Product element) {
if (head == null) {
head = tail = new Node();
head.element = element;
head.next = tail;
tail = head;
}else{
tail.next = new Node();
tail = tail.next;
tail.element = element;
}
counter++;
}
/*public void set(int position, Product element) {
if (position == size()) {
add(element);
return;
} else if (position == 0) {
}
}*/
public void set(int inLayer, Product element) {
if (head == null) {
head = tail = new Node();
head.element = element;
head.next = tail;
tail = head;
}else{
tail.next = new Node();
tail = tail.next;
tail.element = element;
}
counter++;
}
public ListIterator iterator() {
return ListIterator();
}
Node current = head;
boolean hasNext() {
return (current.next != null);
}
Product next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
current = current.next;
return current.value;
}