判断是否为空链表: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()); } }
判断是否为空链表
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()); } }
分类:
Java
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ASP.NET Core 模型验证消息的本地化新姿势
· 从零开始开发一个 MCP Server!
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· Ai满嘴顺口溜,想考研?浪费我几个小时
· Browser-use 详细介绍&使用文档
2017-11-17 Macached-php-Macache插件安装