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

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

在这里插入图片描述

问题描述:

求单链表有效节点的个数。

解决方案:

package LinkedList.test;/** * @version 1.0 * @auther WangCode * @date 2021/4/3 16:08 */public class SingleLinkedListTest {       public static void main(String[] args) {           Node n1 = new Node(1, "123");        Node n2 = new Node(1, "123");        Node n3 = new Node(1, "123");        LinkedList linkedList = new LinkedList();        linkedList.add(n1);        linkedList.add(n2);        linkedList.add(n3);        System.out.println(linkedList.getLength(linkedList.getHead()));    }}//定义一个节点class Node{       public int no;    public String data;    public Node next;    //构造方法    public Node(int no, String data){           this.no = no;        this.data = data;    }    @Override    public String toString() {           return "Node{" +                "no=" + no +                ", data='" + data + '\'' +                '}';    }}//创建单链表class LinkedList{       //初始化头节点    private Node head = new Node(0,"");    public Node getHead() {           return head;    }    /**     * 添加节点     * @param node     */    public void add(Node node){           Node temp = head;        while (true){               if (temp.next == null){   //找到链表最后一个节点                break;            }            temp = temp.next;        }        temp.next = node;//在链表的最后添加新的节点    }    /**     * 遍历链表     */    public void list(){           Node temp = head.next;        if (temp == null){               System.out.println("该链表为空");            return;        }        while(true){               if (temp == null){                   break;            }            System.out.println(temp);            temp = temp.next;        }    }    /**     * 获取单链表的长度     * @param head 单链表的头节点     * @return 返回单链表的有效长度     */    public int getLength(Node head){           if (head.next == null){               return 0;        }        int length = 0;        Node temp = head.next;        while(temp != null){               length++;            temp = temp.next;        }        return length;    }}

链表是由节点构成的,我们先创建我们的节点类;

创建单链表,添加了三个功能,添加节点(尾部添加)、遍历链表、返回链表有效长度;

public int getLength(Node head){           if (head.next == null){   //这里我们定义的头节点是个空节点,所以此时链表为空            return 0;        }        int length = 0;//定义长度        Node temp = head.next;//辅助指针        while(temp != null){   // 一直遍历到链表的尾部            length++;            temp = temp.next;        }        return length;    }

运行结果:

在这里插入图片描述
所实话这个蛮简单的;

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

你可能感兴趣的文章
Mysql 报错 Field 'id' doesn't have a default value
查看>>
MySQL 报错:Duplicate entry 'xxx' for key 'UNIQ_XXXX'
查看>>
Mysql 拼接多个字段作为查询条件查询方法
查看>>
mysql 排序id_mysql如何按特定id排序
查看>>
Mysql 提示:Communication link failure
查看>>
mysql 插入是否成功_PDO mysql:如何知道插入是否成功
查看>>
Mysql 数据库InnoDB存储引擎中主要组件的刷新清理条件:脏页、RedoLog重做日志、Insert Buffer或ChangeBuffer、Undo Log
查看>>
mysql 数据库中 count(*),count(1),count(列名)区别和效率问题
查看>>
mysql 数据库备份及ibdata1的瘦身
查看>>
MySQL 数据库备份种类以及常用备份工具汇总
查看>>
mysql 数据库存储引擎怎么选择?快来看看性能测试吧
查看>>
MySQL 数据库操作指南:学习如何使用 Python 进行增删改查操作
查看>>
MySQL 数据库的高可用性分析
查看>>
MySQL 数据库设计总结
查看>>
Mysql 数据库重置ID排序
查看>>
Mysql 数据类型一日期
查看>>
MySQL 数据类型和属性
查看>>
mysql 敲错命令 想取消怎么办?
查看>>
Mysql 整形列的字节与存储范围
查看>>
mysql 断电数据损坏,无法启动
查看>>