import java.util.NoSuchElementException;
class LList<E> implements List<E> {
private Link<E> head;
private Link<E> tail;
private Link<E> curr;
private int listSize;
LList(int size) {
this();
}
LList() {
clear();
}
public void clear() {
curr = tail = new Link<E>(null);
head = new Link<E>(tail);
listSize = 0;
}
public boolean insert(E it) {
curr.setNext(new Link<E>(curr.element(), curr.next()));
curr.setElement(it);
if (tail == curr) {
tail = curr.next();
}
listSize++;
return true;
}
public boolean append(E it) {
tail.setNext(new Link<E>(null));
tail.setElement(it);
tail = tail.next();
listSize++;
return true;
}
public E remove () throws NoSuchElementException {
if (curr == tail) {
throw new NoSuchElementException("remove() in LList has current of " + curr + " and size of "
+ listSize + " that is not a a valid element");
}
E it = curr.element();
curr.setElement(curr.next().element());
if (curr.next() == tail) {
tail = curr;
}
curr.setNext(curr.next().next());
listSize--;
return it;
}
public void moveToStart() {
curr = head.next();
}
public void moveToEnd() {
curr = tail;
}
public void prev() {
if (head.next() == curr) {
return;
}
Link<E> temp = head;
while (temp.next() != curr) {
temp = temp.next();
}
curr = temp;
}
public void next() { if (curr != tail) { curr = curr.next(); } }
public int length() { return listSize; }
public int currPos() {
Link<E> temp = head.next();
int i;
for (i=0; curr != temp; i++) {
temp = temp.next();
}
return i;
}
public boolean moveToPos(int pos) {
if ((pos < 0) || (pos > listSize)) {
return false;
}
curr = head.next();
for(int i=0; i<pos; i++) { curr = curr.next(); }
return true;
}
public boolean isAtEnd() { return curr == tail; }
public E getValue() throws NoSuchElementException {
if (curr == tail)
{
throw new NoSuchElementException("getvalue() in LList has current of " + curr + " and size of "
+ listSize + " that is not a a valid element");
}
return curr.element();
}
public boolean isEmpty() {
return listSize == 0;
}
}