开发者社区 问答 正文

如何判断是否为空链表?

已解决

如何判断是否为空链表?

展开
收起
出笼的鸟 2022-04-02 23:32:49 1102 分享 版权
1 条回答
写回答
取消 提交回答
  • 推荐回答

    判断是否为空链表:public boolean isEmpty() 如果根元素为null或者保存的元素个数为0,

    则认为链表是一个空链表,则isEmpty()返回的就应该是一个true。

    public boolean isEmpty() {

    return this.root =null &&this.count ==0 ;

    }

    }

    public class TestLinkDemo {

    public static void main (String args[]) throws Exception

    Link all = new Link ();

    system.out.println (all.size()r+ " =" + all.isEmpty ((all.size()r+ " =" + all.isEmpty());

    al1 .add ( "hello") ;

    al1.add ( "world" );

    all.add ( "mldn" );

    System.out.println (all.size() + " =" +all.isEmpty ((all.size() + " =" +all.isEmpty())

    }

    }

    2022-04-03 09:16:01
    赞同 展开评论
问答标签:
问答地址: