栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

单向链表思路

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

单向链表思路

package linkedList;

import java.util.Iterator;
import java.util.Objects;
import java.util.Spliterator;
import java.util.function.Consumer;

public class oneWaylinkedList implements Iterable {
    
    public static class Node {
        public int data;

        public Node next;

        public Node(int data) {
            this.data = data;
        }
    }

    private Node first = null;

    private Node current = null;

    
    public void add(int data) {
        //产生一个新节点
        Node node = new Node(data);
        //1、如果是第一个节点
        if (Objects.isNull(first)) {
            first = node;
            current = node;
        } else {
            //2、如果不是第一个节点
            current.next = node;
            current = node;
        }
    }

    
    public void del(int position) {
        //如果要删除的是头节点
        if (position == 0) {
            first = first.next;
        }
        int curPosition = 1;
        Node preNode = first;
        Node curNode = first.next;
        while (Objects.nonNull(curNode)) {
            if (curPosition == position) {
                preNode.next = curNode.next;
                //清空要删除节点的next
                curNode.next = null;
            }
            curPosition++;
            preNode = curNode;
            curNode = curNode.next;
        }
    }

    
    public void display() {
        Node node = first;
        while (Objects.nonNull(node)) {
            System.out.println(node.data);
            node = node.next;
        }
    }

    
    public Node getStartNode() {
        return first;
    }

    
    public Node getEndNode() {
        if (Objects.isNull(first)){
            return null;
        }
        Node node = first;
        while (Objects.nonNull(node.next)) {
            node = node.next;
        }
        return node;
    }

    
    public Node get(int position) {
        if (position == 0) {
            return first;
        }
        int curPosition = 1;
        Node curNode = first.next;
        while (Objects.nonNull(curNode)) {
            if (curPosition == position) {
                return curNode;
            }
            curPosition++;
            curNode = curNode.next;
        }
        return null;
    }

    @Override
    public Iterator iterator() {
        //匿名内部类
        return new Iterator() {
            Node node = first;//外部类中的私有成员

            @Override
            public void remove() {
                Iterator.super.remove();
            }

            @Override
            public void forEachRemaining(Consumer action) {
                Iterator.super.forEachRemaining(action);
            }

            @Override
            public boolean hasNext() {
                return Objects.nonNull(node);
            }

            @Override
            public Object next() {
                int data = node.data;
                node = node.next;
                return data;
            }
        };
    }

    @Override
    public void forEach(Consumer action) {
        Iterable.super.forEach(action);
    }

    @Override
    public Spliterator spliterator() {
        return Iterable.super.spliterator();
    }

}

图解:

思路:首先要理解一个节点包含两部分,其中data是数据,next是指向下一个节点。故链表的数据连接起来其实是通过next。知道了节点的含义及内容后,我们可以去写一个Node类封装节点 。再建一个类去编写链表,其实链表只需要知道头节点和当前节点。也就是说我们向链表插入第一个节点a时,a既是头节点也是当前节点,继续向链表插入节点b时,a依旧是头节点,但是此时我们需要做两个操作,第一个是将节点a中的next指向节点b,第二个是更改当前节点为b。以此类推便形成了单向链表,所以链表中最后一个节点的next是null,没有指向了。

操作:链表的增删改查,重点讲一下删除,若链表此时是a—b—c—d四个节点,想要删除节点c,那么需要直接更改节点b的next,将其指向节点d并将节点c的next致空。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/713499.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号