zoukankan      html  css  js  c++  java
  • 自定义单链表(六)

    链表和数组区别,链表不是顺序存储。每一个节点里面存着下一个节点的指针。

    优点:无需实现申明大小,插入方便。

    缺点:查询速度比不上数组。

    单链表插入节点

    1、找到该链表的最后一个节点

    2、最后一个节点的指针指向新节点。

    3、返回自身。

    代码

    public class Node {
    
        int data;
        Node next;
    
        public Node(int data) {
            this.data = data;
        }
            
        public Node add(Node node) {
            Node curNode = this;
            // 找到该链表最后一个节点
            while (true) {
                Node nextNode = curNode.next;
                // 如果节点为空 意味着curNode为最后一个节点
                if (nextNode == null) {
                    break;
                }
                curNode = nextNode;
            }
            // 最后一个节点下一个节点指向 node
            curNode.next = node;
            return this;
        }
    }

    删除节点。

    1、取出该节点的下下节点。

    2、把下下节点赋值给当前节点的下个节点。

    3、返回删除数据。

    代码实现

    public class Node {
    
        int data;
        Node next;
    
        public Node(int data) {
            this.data = data;
        }
    
        // 删除节点
        public int remove() {
            // 先取出下下个节点
            int result = next.data;
            Node nextNode = next.next;
            this.next = nextNode;
            return result;
        }
    
    }

    查找元素

    1、当前节点的值和目标元素的值进行比较,相等返回该节点,不相同进行下一步。

    2、节点向后移动一位,继续进行比较。

    代码实现

    public class Node {
    
        int data;
        Node next;
    
            public Node find(int data) {
            Node current = this;
            while (current != null) {
                if (data == current.data) {
                    return current;
                }
                current = current.next;
            }
            return null;
        }
    
    }

    所有代码

    public class Node {
    
        int data;
        Node next;
    
        public Node(int data) {
            this.data = data;
        }
    
        public Node add(Node node) {
            Node curNode = this;
            // 找到该链表最后一个节点
            while (true) {
                Node nextNode = curNode.next;
                // 如果节点为空 意味着curNode为最后一个节点
                if (nextNode == null) {
                    break;
                }
                curNode = nextNode;
            }
            // 最后一个节点下一个节点指向 node
            curNode.next = node;
            return this;
        }
    
        // 删除节点
        public int remove() {
            // 先取出下下个节点
            int result = next.data;
            Node nextNode = next.next;
            this.next = nextNode;
            return result;
        }
    
        public Node find(int data) {
            Node current = this;
            while (current != null) {
                if (data == current.data) {
                    return current;
                }
                current = current.next;
            }
            return null;
        }
    
        // 打印节点
        public void show() {
            Node curNode = this;
            while (true) {
                System.out.print(curNode.data + " ");
                Node nextNode = curNode.next;
                // 如果节点为空
                if (nextNode == null) {
                    break;
                }
                curNode = nextNode;
            }
            System.out.println();
        }
    
        public Node next() {
            return this.next;
        }
    
        public int getData() {
            return data;
        }
    
    }

    测试代码

    public class NodeTest {
    
        public static void main(String[] args) {
            Node node = new Node(1);
            Node node2 = new Node(2);
            Node node3 = new Node(3);
            Node node4 = new Node(4);
            node.add(node2).add(node3).add(node4);
            node.show();
            System.out.println(node3.remove());
            node.show();
            
            System.out.println(node.find(3).data);
    
        }
    }
  • 相关阅读:
    在sql语句中使用 xml for path 格式化字符串的方法总结
    Android handler的使用简单示例
    easyui datagrid中 多表头方法总结
    使用ICSharpCode.SharpZipLib.Zip类库解压zip文件的方法
    ThreadPoolExecutor 优雅关闭线程池的原理.md
    ThreadPoolExecutor 几个疑惑与解答
    如何在运行时(Runtime)获得泛型的真正类型
    为什么 EXISTS(NOT EXIST) 与 JOIN(LEFT JOIN) 的性能会比 IN(NOT IN) 好
    Spring MVC 上下文(ApplicationContext)初始化入口
    Tomcat生成的session持久化到MySQL
  • 原文地址:https://www.cnblogs.com/laolei11/p/10642976.html
Copyright © 2011-2022 走看看