public class LinkList {
private Node firstNode; //head
private Node currentLastNode;//tail
private int size = 0;
/*public void add(Object o) {
if (firstNode == null) {
Node n = new Node();
n.setData(o);
firstNode = n;
currentLastNode = n;
size++;
} else {
Node n = new Node();
n.setData(o);
currentLastNode.setNext(n);
currentLastNode = n;
size++;
}
}*/
public void add(Object o) {
Node n = new Node();
if (firstNode == null) {
n.setData(o);
firstNode = n;
currentLastNode = n;
} else {
n.setData(o);
currentLastNode.setNext(n);
currentLastNode = n;
}
size++;
}
public int size() {
return size;
}
public String toString() {
Node n = firstNode;
String s = "";
while(n != null){
s = s + n.getData() + " ";
n = n.getNext();
}
return s;
}
}
import static org.junit.Assert.*;
import mypack4.LinkList;
import org.junit.Test;
public class LinkListTest {
@Test
public void test() {
LinkList l = new LinkList();
for (int i = 0; i < 15; i++) {
l.add(i);
}
System.out.println(l);
System.out.println(l.size());
}
}