public class Node {
private Object data;
private Node next;
public Node(Object data, Node next) {
super();
this.data = data;
this.next = next;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
public class LinkedList {
private Node head;
private Node tail;
private int size;
public void add(Object obj) {
Node node = new Node(obj, null);
if (head == null) {
head = node;
tail = node;
}
tail.setNext(node);
tail = node;
size++;
}
public int size() {
return size;
}
}
junit test code:
@Test
public void testAdd() {
LinkedList linkedList = new LinkedList();
for (int i = 0; i < 15; i++) {
linkedList.add(new Cat(i));
}
assertTrue(linkedList.size()==15);
}
public class LinkNode {
private Object data;
private LinkNode right;
private LinkNode left;
public LinkNode(Object data, LinkNode right, LinkNode left) {
super();
this.data = data;
this.right = right;
this.left = left;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public LinkNode getRight() {
return right;
}
public void setRight(LinkNode right) {
this.right = right;
}
public LinkNode getLeft() {
return left;
}
public void setLeft(LinkNode left) {
this.left = left;
}
}
public class Link {
private LinkNode head;
private LinkNode tail;
private int size;
public void add(Object obj) {
LinkNode node = new LinkNode(obj, null, null);
if (head == null) {
head = node;
tail = head;
head.setRight(tail);
head.setLeft(tail);
}
tail.setRight(node);
node.setLeft(tail);
tail = node;
tail.setRight(head);
head.setLeft(tail);
size++;
}
public int size() {
return size;
}
public LinkNode getTailNode() {
return tail;
}
public LinkNode getHeadNode() {
return head;
}
}
junit test code:
@Test
public void testAdd() {
Link link = new Link();
for (int i = 0; i < 15; i++) {
link.add(new Cat(i));
}
assertTrue(link.size()==15);
assertEquals(link.getHeadNode().getLeft().getData(),link.getTailNode().getData());
assertEquals(link.getTailNode().getRight().getData(),link.getHeadNode().getData());
}