zoukankan      html  css  js  c++  java
  • 链表05-开发可用链表(判断是否为空链表)

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

  • 相关阅读:
    Nhibernate 3.0 cookbook学习笔记 配置与架构
    jQuery 三级联动选项栏
    依赖注入框架Autofac学习笔记
    Windows服务初探
    再记面试题
    Nhibernate 3.0 cookbook学习笔记 一对多与多对多映射
    Nhibernate 3.0 cookbook学习笔记 创建一个加密类
    2011 微软 MVP 全球大会即将拉开序幕
    Windows Shell扩展系列文章 2 .NET 4为扩展的Windows Shell上下文菜单项添加位图图标
    【转】微软一站式示例代码库(中文版)20110413版本, 新添加16个Sample
  • 原文地址:https://www.cnblogs.com/anyux/p/11876436.html
Copyright © 2011-2022 走看看