package com.fill.model1;
public class Tlist {
public static void main(String[] args) {
// TODO Auto-generated method stub
//System.out.println(jsd(5));
NodeManage nm=new NodeManage();
nm.addNode("节点1");
nm.addNode("节点2");
nm.addNode("节点3");
nm.addNode("节点4");
nm.printNode();
nm.delNode("节点8");
nm.printNode();
}
//list链表
public static int jsd(int num){
if(num>1){
return num*jsd(num-1);
}else{
return num;
}
}
}
//管理链表
class NodeManage{
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.println(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.println("-->"+this.next.name);
this.next.print();
}
}
}
}