java链表
package com.phz.test1;
public class Link {
class Node{
private String data;
private Node next;
public Node(String data){
this.data = data;
}
//添加节点
public void add(Node newNode){
if(this.next==null)this.next=newNode;
else this.next.add(newNode);
}
//打印节点
public void print(){
System.out.print(this.data+"\t");
if(this.next!=null)this.next.print();
}
//查询节点
public boolean search(String data){
if(data.equals(this.data))return true;
else {
if(this.next!=null)return this.next.search(data);
else return false;
}
}
//删除节点
public void delete(Node previous,String data){
if(data.equals(this.data))previous.next = this.next;
else{
if(this.next!=null)this.next.delete(this, data);
}
}
}//Node
private Node root;
public void addNode(String data){
Node newNode = new Node(data);
if(this.root==null)this.root=newNode;
else this.root.add(newNode);
}
public void printNode(){
if(this.root!=null)this.root.print();
}
public boolean contains(String name){
return this.root.search(name);
}
public void deleteNode(String data){
if(this.contains(data)){
if(this.root.data.equals(data))this.root = this.root.next;
else this.root.next.delete(root, data);
}
}
//test main
public static void main(String[] args) {
Link l = new Link();
l.addNode("A");
l.addNode("B");
l.addNode("C");
l.addNode("D");
l.addNode("E");
l.printNode();
l.deleteNode("C");
System.out.println();
l.printNode();
System.out.println("查询节点:"+l.contains("A"));
}
}//Link