Fork me on GitHub
随笔 - 234  文章 - 49 评论 - 0 阅读 - 13万
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

判断是否为空链表: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());
    }
}
复制代码

 

 

 

 

 

posted on   anyux  阅读(803)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ASP.NET Core 模型验证消息的本地化新姿势
阅读排行:
· 从零开始开发一个 MCP Server!
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· Ai满嘴顺口溜,想考研?浪费我几个小时
· Browser-use 详细介绍&使用文档
历史上的今天:
2017-11-17 Macached-php-Macache插件安装
点击右上角即可分享
微信分享提示