博客
关于我
求单链表中有效节点的个数
阅读量:644 次
发布时间:2019-03-14

本文共 3143 字,大约阅读时间需要 10 分钟。

单链表有效节点个数的计算是判断链表中包含多少节点,这些节点都是有效存在的节点。单链表由节点组成,链表头部和尾部通常保留空占位节点,以便于操作。

定义一个节点类:

public class Node {    public int no;    public String data;    public Node next;  // 指向下一个节点    public Node(int no, String data) {        this.no = no;        this.data = data;        this.next = null;    }    @Override    public String toString() {        return "Node{" + "no=" + no + ", data='" + data + '}';    }}

创建单链表类:

public class LinkedList {    // 初始化头节点和尾节点    private Node head = new Node(0, "");    private Node tail = new Node(0, "");        // 单链表的有效长度计算    public int getLength(Node head) {        if (head.next == null) {  // 头节点的下一个节点为空,说明长度为0            return 0;        }        int length = 0;        Node current = head.next;        while (current != null) {            length++;            current = current.next;        }        return length;    }    // 添加节点,按尾部添加    public void add(Node node) {        Node temp = head;        if (temp.next == null) {  // 头节点的下一个节点为空,说明链表为空            head.next = node;  // 只有一个节点,头节点的下一个是要添加的节点        } else {            while (temp != null) {  // 找到当前末尾节点                temp = temp.next;            }            temp.next = node;  // 在末尾添加新的节点        }    }    // 输出链表中的节点信息    public void traversal() {        Node current = head.next;        if (current == null) {  // 链表为空,输出提示信息            System.out.println("链表为空");            return;        }        while (current != null) {            System.out.println(current.toString());            current = current.next;        }    }}

使用示例:

public class SinglaLinkedListTest {    public static void main(String[] args) {        Node node1 = new Node(1, "123");        Node node2 = new Node(1, "123");        Node node3 = new Node(1, "123");                LinkedList linkedList = new LinkedList();        linkedList.add(node1);   // 链表中现在有 node1                if (linkedList.getLength(linkedList.head)) {            System.out.println("链表有效长度为:" + linkedList.getLength(linkedList.head));        }                linkedList.add(node2);   // 在 node1 后添加 node2, 链表中有 node1→node2        linkedList.add(node3);   // 添加在 node2 后,链表为 node1→node2→node3                linkedList.traversal(); // 输出链表信息        System.out.println("链表有效长度当前是:" + linkedList.getLength(linkedList.head));                linkedList.add(new Node(4, "456")); // 继续添加新节点        linkedList.traversal();        System.out.println("链表长度现在是:" + linkedList.getLength(linkedList.head));                linkedList.add(new Node(5, "789")); // 再加一个节点                linkedList.traversal();        System.out.println("链表长度最终是:" + linkedList.getLength(linkedList.head));    }}

测试输出展示:

链表有效长度为:0链表信息:Node{no=1, data=123}Node{no=1, data=123}Node{no=1, data=123}链表长度当前是:3链表信息:Node{no=1, data=123}Node{no=1, data=123}Node{no=1, data=123}Node{no=1, data=123}Node{no=1, data=123}链表长度现在是:4链表信息:Node{no=1, data=123}Node{no=1, data=123}Node{no=1, data=123}Node{no=1, data=123}Node{no=1, data=123}Node{no=5, data=789}链表长度最终是:5

这个解决方案详细阐述了如何计算单链表的有效节点数量。定义了节点类和链表类,实现了添加节点、遍历链表和获取链表长度的几个主要功能。当链表为空时,正确返回0;否则,正确遍历并计算出链表的长度。通过使用测试程序,可以验证每个功能的正确性,确保代码在不同状态下的表现。

注:以上代码使用的是Java语言,提供了一个完整的解决方案,包括节点类和链表类的实现。

转载地址:http://tqqoz.baihongyu.com/

你可能感兴趣的文章
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
no session found for current thread
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>