LinkedListBuilder<E>
class
class LinkedListBuilder<E> {
LinkedList<E> _first = null;
Cons<E> _last = null;
void add(E x) {
Cons<E> cons = new Cons<E>(x, null);
if (_first == null) {
_first = cons;
} else {
_last.tail = cons;
}
_last = cons;
}
LinkedList<E> build([tail = null]) {
if (tail == null)
tail = new Nil<E>();
if (_first == null) {
return tail;
} else {
_last.tail = tail;
return _first;
}
}
}
Methods
void add(E x) #
void add(E x) {
Cons<E> cons = new Cons<E>(x, null);
if (_first == null) {
_first = cons;
} else {
_last.tail = cons;
}
_last = cons;
}
LinkedList<E> build([tail = null]) #
LinkedList<E> build([tail = null]) {
if (tail == null)
tail = new Nil<E>();
if (_first == null) {
return tail;
} else {
_last.tail = tail;
return _first;
}
}