class Node{
    private String data ;    //节点内容
    private Node next ;        //保存下一个节点
    public Node(String data){
        this.data = data ;
    }
    public void setNext(Node next){            //设置下一个节点
        this.next = next ;
    }
    public Node getNext(){            //取得下一个节点内容
        return next ;
    }
    public void setData(String data){
        this.data = data ;
    }
    public String getData(){
        return data ;
    }
    public void print(){
        System.out.print(this.getData()+"\t") ;
        if(this.getNext()!=null){
            this.getNext().print() ;
        }
    }
}
class LinkDemo01{
    public static void main(String [] args){
        Node nd1 = null ;
        Node nd2 = null ;
        Node nd3 = null ;
        Node nd4 = null ;
        Node nd5 = null ;
        nd1 = new Node("火车头") ;
        nd2 = new Node("A");
        nd3 = new Node("B");
        nd4 = new Node("C");
        nd5 = new Node("D");
        nd1.setNext(nd2) ;
        nd2.setNext(nd3) ;
        nd3.setNext(nd4) ;
        nd4.setNext(nd5) ;
        nd1.print() ;
    }
}

单向链表的输出须使用递归!

 

//增加节点
//插入节点
//删除节点
class Link{                //链表的完成类
    class Node{            //保存每个节点,此处为了方便直接定义为内部类
        private String data ;        //保存节点的内容
        private Node next ;            //保存下一个节点
        Node(String data){            
            this.data = data ;        //通过构造方法设置节点的内容
        }
        public void add(Node newNode){        //将结果加入到合适的位置去
            if(this.next == null){            //如果下一个节点为空,则把节点设置在next的位置
                this.next = newNode ;    
            }else{            //如果不为空,则需要想下继续寻找节点
                this.next.add(newNode) ;
            }
        }
        public void print(){            
            System.out.print(this.data+"\t" ) ;        //输入节点内容
            if(this.next!=null){            //还有下一个元素,需继续输出
                this.next.print() ;            //下一个节点继续调用print
            }
        }
        public boolean search(String data){        //查询值是否存在
            if(this.data.equals(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){        //在开发中必须考虑2种情况
        Node newNode = new Node(data) ;
        if(root == null){
            this.root = newNode ;    //如果没有根节点,把这个节点设置为根节点
        }else{        //如果不是根节点,则把其挂在最后节点之后
            this.root.add(newNode) ;
        }
    } 
    public void printNode(){        //输出全部链表内容
        if(this.root!=null){        //如果根元素不为空
            this.root.print() ;    //调用Node类中的输出方法
        }
    }
    public boolean contains(String name){        //判断元素是否存在
        return this.root.search(name) ;            //调用Node节点类中的查找方法
    }
    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 LinkDemo02{
    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") ;        //增加节点
        System.out.println("============删除之前==========") ;
        l.printNode() ;            //打印输出链表
        //System.out.print(l.contains("A")) ;        //判断元素A是否存在
        //System.out.print(l.contains("X")) ;        //判断元素A是否存在
        System.out.println("\n==========删除之后==========") ;
        l.deleteNode("C") ;        //删除节点
        l.printNode() ;
    }
}

 

posted on 2014-04-24 18:56  似_水流年  阅读(93)  评论(0)    收藏  举报