节点类
package link;
/**
* 节点类
* @author Administrator
*
*/
public class Node {
long data;
Node next;
public Node(long value){
this.data = value;
}
/**
* 显示
*/
public void display(){
System.out.print(data+" ");
}
}
链表类
package link;
public class LinkList {
private Node first;
/**
* 插入一个节点
*/
public void insertNode(long value){
Node node = new Node(value);
node.next = first;
first = node;
}
/**
* 显示链表
*/
public void display(){
Node current = first;
while(current != null){
current.display();
current = current.next;
}
System.out.println();
}
/**
* 删除一个节点
*/
public void deleteFirst(){
Node tmp = first;
first = tmp.next;
}
/**
* 查找节点
*/
public Node find(long value){
Node current = first;
while(current.data != value){
if(current.next == null){
return null;
}
current = current.next;
}
return current;
}
/**
* 根据值删除节点
*/
public void delete(long value){
Node current = first;
Node tmp = first;
while(current.data != value){
if(current.next == null){
return;
}
tmp = current;
current = current.next;
}
if(current == first){
first = current.next;
}else{
tmp.next = current.next;
}
}
}