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);
    
        }
    }
  • 相关阅读:
    hdu 2019 数列有序!
    hdu 2023 求平均成绩
    HDU 5805 NanoApe Loves Sequence (思维题) BestCoder Round #86 1002
    51nod 1264 线段相交
    Gym 100801A Alex Origami Squares (求正方形边长)
    HDU 5512 Pagodas (gcd)
    HDU 5510 Bazinga (字符串匹配)
    UVALive 7269 Snake Carpet (构造)
    UVALive 7270 Osu! Master (阅读理解题)
    UVALive 7267 Mysterious Antiques in Sackler Museum (判断长方形)
  • 原文地址:https://www.cnblogs.com/laolei11/p/10642976.html
Copyright © 2011-2022 走看看