目录
Single Linked List
构造链表
public class Node {
public Object value;
public Node next;
public Node(Object value,Node next) {
this.value=value;
this.next=next;
}
public Node(Object value) {
this.value=value;
this.next=null;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
在表头插入结点
public class SingleLikedList {
private Node head;
private Node current;
private int size;
public SingleLikedList() {
head = current = new Node(null);
size=0;
}
public boolean isEmpty(){
return size==0;
}
public int getSize(){
return size;
}
public void addFirst(Object obj) {
Node temp = head;
head=new Node(obj,temp);
size++;
}
}
在表尾插入结点
接上
public void addLast(Object obj) {
Node newNode = new Node(obj);
current=head.next;
//找到当前尾结点
while(current.next != null