zoukankan      html  css  js  c++  java
  • 简单链表实例

    public class Link {
        class Node { // 把节点类定义成内部类
            private String data;
            private Node next;
    
            public Node(String Data) {
                this.data = data;
            }
    
            /**
             * 添加节点的方法
             */
            public void add(Node newNode) { // 增加add方法
                if (this.next == null) { // 判断下一个节点是否为空
                    this.next = newNode;// 如果下一个节点为空,则设置下一个节点为newNode
                } else { // 如果不为空
                    this.next.add(newNode);// 调用下一个节点的add方法
                }
            }
    
            /**
             * 打印方法
             */
            public void print() {
                System.out.print(this.data+"	");
                if (this.next != null) {    //如果下一个节点不为空,则继续打印
                    this.next.print();    //输出下一个节点
                }
            }
    
            /**
             * 搜索方法
             */
            public boolean search(String data) {//内部定义搜索方法
                if (data.equals(this.data)) {  //判断当前节点的名字是否与查找的一致
                    return true;          //如果一直,返回true
                } else {              //继续判断下一个
                    if (this.next != null) {  //下一个节点存在则继续查找
                        return this.next.search(data);//返回下一个的查询结果
                    }
                }
                return false;      //没找到,则返回false
            }
    
            /**
             * 删除节点
             */
            public void delete(Node Previous, String data) {
                if (data.equals(this.data)) {      //找到了匹配的节点
                    Previous.next = this.next;      //空出当前节点
                } else {
                    if (this.next != null) {
                        this.next.delete(this, data);  //继续向下找
                    }
                }
            }
        }
    
        private Node root; // 表示根节点
    
        /**
         * 添加方法
         */
        public void addNode(String data) {
            Node newNode = new Node(data);    //建立一个新的节点
            if (this.root == null) {        //判断是否有根节点
                this.root = newNode;        //赋值根节点
            } else {
                this.root.add(newNode);      //添加到合适位置
            }
        }
    
        /**
         * 输出所有信息
         */
        public void printNode() {
            if(this.root!=null){      //判断是否存在根节点
                this.root.print();      //输出所有节点信息
            }
        }
        /**
         * 判断是否包含某元素
         */
        public boolean contains(String data){
            return this.root.search(data);      //调用Node类的search方法
        }
        /**
         * 删除方法
         */
        public void deleteNode(String data){
            if(this.contains(data)){        //判断是否存在查找的信息
                if(this.root.data.equals(data)){  //判断符合的节点是否为根节点
                    this.root=this.root.next;    //将根节点之后的内容设置为根节点
                }else{
                    this.root.next.delete(root, data);    //删除节点
                }
            }
        }
    }
  • 相关阅读:
    九度 1363 欢乐斗地主
    九度 1377 缓变序列
    九度 1376 最近零子序列
    转几篇关于linux下AT&T汇编的帖子
    九度 1358 陈博的平均主义
    九度 1394 五连击数组
    HDU 2817 A sequence of numbers
    HDU 1867 A + B for you again
    HDU 1753 大明A+B
    HDU 1715 大菲波数
  • 原文地址:https://www.cnblogs.com/ak666/p/8073803.html
Copyright © 2011-2022 走看看