import java.util.NoSuchElementException;
Basic doubly linked list (with no header nor trailer)
@author
public class BasicDLList<E> implements List<E> {
private Link<E> head;
private Link<E> tail;
private Link<E> curr;
private int listSize;
BasicDLList(int size) {
this();
}
BasicDLList() {
clear();
}
public void clear() {
curr = tail = null;
head = null;
listSize = 0;
}
public boolean insert(E it){
curr = new Link( it, curr.prev(), curr );
curr.prev().setNext( curr );
curr.next().setPrev( curr );
listSize++;
return true;
}
public boolean append(E it){
tail.setPrev( new Link( it, tail.prev(), tail ) );
tail.prev().prev().setNext( tail.prev() );
if( curr == tail ){
curr = tail.prev();
}
listSize++;
return true;
}
public E remove () throws NoSuchElementException {
if( curr == tail ){
return null;
}
E it = curr.element();
curr.prev().setNext( curr.next() );
curr.next().setPrev( curr.prev() );
curr = curr.next();
listSize--;
return it;
}
public void moveToStart() {
curr = head.next();
}
public void moveToEnd() {
curr = tail;
}
public void prev() {
if (head.next() == curr) {
return;
}
curr = curr.prev();
}
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 BasicDLList has current of " + curr + " and size of "
+ listSize + " that is not a a valid element");
}
return curr.element();
}
public boolean isEmpty() {
return listSize == 0;
}
}