/** * 数据结构之链表(单向链表) * @author Administrator * */ public class LinkNodeTest { public static void main(String[] args) { LinkManager manager = new LinkManager(); manager.addNode("节点1"); manager.addNode("节点2"); manager.addNode("节点3"); manager.addNode("节点4"); manager.addNode("节点5"); manager.printNode(); manager.delNode("节点3"); manager.printNode(); } } class LinkManager{ private Node root;//根节点 //添加节点 public void addNode(String name){ if(root==null){ root = new Node(name); }else{ root.add(name); } } //删除节点 public void delNode(String name){ if(root!=null){ if(root.name.equals(name)){ root = root.next; }else{ root.del(name); } } } //输出所有节点 public void printNode(){ if(root != null){ System.out.print(root.name); root.print(); System.out.println(); } } //节点内部类 class Node{ private String name; private Node next; public Node(String name){ this.name = name; } //添加 public void add(String name){ if(this.next==null){ this.next = new Node(name); }else{ this.next.add(name); } } //删除 public void del(String name){ if(this.next != null){ if(this.next.name.equals(name)){ this.next = this.next.next; }else{ this.next.del(name); } } } //输出 public void print(){ if(this.next != null){ System.out.print("-->"+this.next.name); this.next.print(); } } } }