zoukankan      html  css  js  c++  java
  • 链表05-开发可用链表(判断是否为空链表)

    判断是否为空链表:public boolean isEmpty()

      空链表判断实际上可以通过两种方式完成:

        第一个:判断root有对象(是不为null)

        第二个:判断保存的数据量(count)

      判断是否为空链表

        public boolean isEmpty(){ //判断链表是否为空
            return this.count == 0;
        }

        本次是一个链表组成内部的剖析,所以一些简单代码一定要清楚实现原理

    class Link{ // 链表类,外部只能看这一个类
        // 定义在内部,主要为Link类服务    
        private class Node{ // 定义的节点类
            private String data; // 保存数据
            private Node next; // 引用关系
            public Node(String data){
                this.data = data;
            }
            public void AddNode(Node newNode){
                if(this.next == null){ // 当前的下一个节点为空
                    this.next = newNode;
                }else{     // 向后继续保存
                    this.next.AddNode(newNode);
                }
            }
    
            public void printNode(){ // 打印Node信息
                System.out.println(this.data);
                if( this.next != null){
                    this.next.printNode();
                }
            }
    
            // ===================以上为内部类============================
        }
        private Node root; // 根结点
        private int count = 0; // 保存元素的个数
        public void add(String data){  // 假设不允许有null
            if (data == null){
                return ;
            }
            Node newNode = new Node(data); // 要保存的数据
            if (this.root == null){ // 如果当前没有根节点,则设置为根节点
                this.root = newNode; // 保存根节点
            }else{ // 存在根节点,则到下一节点找保存数据
                this.root.AddNode(newNode);
            }
            this.count ++; // 每一次保存完成后数量加一
    
        }
        public int size(){ // 取得保存的数据量
            return this.count;
        }
        public boolean isEmpty(){ //判断链表是否为空
            return this.count == 0;
        }
    
        public void print(){ // 打印所有Node信息
            this.root.printNode();
        }
    
    }
    
    public class LinkDemo{
        public static void main(String args[]){
            Link all = new Link();
            System.out.println(all.isEmpty());
            all.add("Hello");
            all.add("World");
            all.add(null);
            all.print();
            all.size();
            System.out.println(all.isEmpty());
        }
    }
    View Code

     

      判断是否为空链表 

        public boolean isEmpty(){ //判断链表是否为空
            return this.root == null;
        }
    class Link{ // 链表类,外部只能看这一个类
        // 定义在内部,主要为Link类服务    
        private class Node{ // 定义的节点类
            private String data; // 保存数据
            private Node next; // 引用关系
            public Node(String data){
                this.data = data;
            }
            public void AddNode(Node newNode){
                if(this.next == null){ // 当前的下一个节点为空
                    this.next = newNode;
                }else{     // 向后继续保存
                    this.next.AddNode(newNode);
                }
            }
    
            public void printNode(){ // 打印Node信息
                System.out.println(this.data);
                if( this.next != null){
                    this.next.printNode();
                }
            }
    
            // ===================以上为内部类============================
        }
        private Node root; // 根结点
        private int count = 0; // 保存元素的个数
        public void add(String data){  // 假设不允许有null
            if (data == null){
                return ;
            }
            Node newNode = new Node(data); // 要保存的数据
            if (this.root == null){ // 如果当前没有根节点,则设置为根节点
                this.root = newNode; // 保存根节点
            }else{ // 存在根节点,则到下一节点找保存数据
                this.root.AddNode(newNode);
            }
            this.count ++; // 每一次保存完成后数量加一
    
        }
        public int size(){ // 取得保存的数据量
            return this.count;
        }
        public boolean isEmpty(){ //判断链表是否为空
            return this.root == null;
        }
    
        public void print(){ // 打印所有Node信息
            this.root.printNode();
        }
    
    }
    
    public class LinkDemo{
        public static void main(String args[]){
            Link all = new Link();
            System.out.println(all.isEmpty());
            all.add("Hello");
            all.add("World");
            all.add(null);
            all.print();
            all.size();
            System.out.println(all.isEmpty());
        }
    }

  • 相关阅读:
    mac下xcode中include path
    如何联机调试和发布程序
    Facebook 错误码
    app store给应用评分功能
    FTP传输模式
    重新安装 Visual Studio 6.0 的问题
    ASP项目:提示用户 'NT AUTHORITY\NETWORK SERVICE' 登录失败
    投资项目的4P
    .net 的资源与本地化
    千万不能干的事(文摘)
  • 原文地址:https://www.cnblogs.com/anyux/p/11876436.html
Copyright © 2011-2022 走看看