package lineartable;
/**
* 链表节点类
*/
class Node{
public Node next;
this.value=value;
this.next=null;
}
}
/**
* 链表类
*/
public class LinkTable {
public Node head;
public LinkTable(Node node){
this.head=node;
}
/**
* 增加节点
*/
public void addNode(Node node){
Node end=head;
while(end.next!=null){
end=end.next;
}
end.next=node;
}
/**
* 删除节点
* 时间复杂度为O(1)无需遍历链表元素
*/
public void delNode(Node node){
node.value=node.next.value;
node.next=node.next.next;
}
/**
* 打印链表元素
*/
public void print(){
Node index=this.head;
while(index!=null){
System.
out.
print(index.
value+"-->");
index=index.next;
}
}
public static void main
(String [] args
){
LinkTable lt=new LinkTable(new Node("中国"));
lt.addNode(new Node("美国"));
Node japanNode=new Node("日本");
lt.addNode(japanNode);
lt.addNode(new Node("意大利"));
lt.addNode(new Node("德国"));
lt.addNode(new Node("荷兰"));
lt.print();
lt.delNode(japanNode);
lt.print();
}
}
/**
运行结果:
中国-->美国-->日本-->意大利-->德国-->荷兰-->NULL
中国-->美国-->意大利-->德国-->荷兰-->NULL
*/
//java/6334