class link
{
class node
{
private String data;
private node next;
public node(String data) //设置节点信息
{
this.data=data;
}
public void add(node newnode) //增加一个add操作
{
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);
}
}
}
};
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);
}
}
}
};
public class test28 {
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("B");
l.deletenode("C");
System.out.println("");
l.printnode();
System.out.println("");
System.out.println("查询节点:"+l.contains("E"));
}
}
{
class node
{
private String data;
private node next;
public node(String data) //设置节点信息
{
this.data=data;
}
public void add(node newnode) //增加一个add操作
{
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);
}
}
}
};
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);
}
}
}
};
public class test28 {
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("B");
l.deletenode("C");
System.out.println("");
l.printnode();
System.out.println("");
System.out.println("查询节点:"+l.contains("E"));
}
}