实体类
Java代码
public class Node {
public Node(){};
public Node(String name){
thisname = name;
}
private String name;
public Node next;
public String getName() {
return name;
}
public void setName(String name) {
thisname = name;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
thisnext = next;
}
}
public class Node {
public Node(){};
public Node(String name){
thisname = name;
}
private String name;
public Node next;
public String getName() {
return name;
}
public void setName(String name) {
thisname = name;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
thisnext = next;
}
}
调用
Java代码
public static void invokeChainMethod(){
Node n = new Node()
nsetName(yang)
nnext = new Node(gao)
Node nx = n;
while(true){
Systemoutprintln(nxgetName())
nx = nxnext;
if(nx == null){
break;
}
}
}
public static void invokeChainMethod(){
Node n = new Node()
nsetName(yang)
nnext = new Node(gao)
Node nx = n;
while(true){
Systemoutprintln(nxgetName())
nx = nxnext;
if(nx == null){
break;
}
}
} 说明
java语言里面链表的实现是通过类来实现的上面的代码显示的是一个最简单的单向链表 不过链表的思想已经体现出来了如果想写双向的链表只需要在Node实体类里面增加一个public Node previous;就行了
java的LinkedList容器的底层实现就是双向的链表结构所以插入删除的时候非常的快!