package demo3;
public class NodeManager {
private Node root;//根节点
//添加节点
public void add(String name){
if(root == null){
root = new Node(name);
}else{
root.add(name);
}
}
//删除节点
public void del(String name){
if(root != null){
if(root.name.equals(name)){
root = root.nextNode;
}else{
root.del(name);
}
}
}
//打印节点
public void print(){
if(root != null){
System.out.print(root.name);
root.print();
}
}
class Node{
private String name;//节点的名字
private Node nextNode;//下一个节点
public Node(String name){
this.name = name;
}
//添加节点
public void add(String name){
if(this.nextNode == null){
this.nextNode = new Node(name);
}else{
this.nextNode.add(name);
}
}
public void del(String name){
if(this.nextNode != null){
if(this.nextNode.name.equals(name)){
this.nextNode = this.nextNode.nextNode;
}else{
this.nextNode.del(name);
}
}
}
//打印节点
public void print(){
if(this.nextNode != null){
System.out.print("->"+this.nextNode.name);
this.nextNode.print();
}
}
}
}
package demo3;
import demo3.NodeManager.Node;
public class Test {
public static void main(String[] args) {
NodeManager manager = new NodeManager();
manager.add("节点1");
manager.add("节点2");
manager.add("节点3");
manager.add("节点4");
manager.add("节点5");
manager.print();
System.out.println();
manager.del("节点1");
manager.del("节点4");
manager.print();
}
}