模拟LinkedList实现的链表(无哨兵)

1.前言

我们将LinkdList视作链表, 底层设计了内部类Node类, 我这里依然没有用到泛型, 其实加上泛型依然很简单, 即将Node节点的数据域的类型由Int转换为E(<E>), 我在此不做赘述.同时实现了增删查改, 遍历等操作.

2.链表(无哨兵)的代码实现

java 复制代码
public class LinkListTest implements Iterable<Integer>{
    //头指针
    static Node head;
    //内部类
    private static class Node{
        //数据域
        int value;
        //指针域
        Node next;
        public Node(int value, Node nest) {
            this.value = value;
            this.next = nest;
        }
    }
    //头插法
    public void addHead(int value) {
        Node p = new Node(value, null);
        p.next = head;
        head = p;
    }
    //遍历链表 : 方式1
    public void Traversal1() {
        Node p = head;
        while (p != null) {
            System.out.println("该节点的值是" + p.value);
            p = p.next;
        }
    }
    //获取指定索引位置上的节点的值
    public int get(int index) {
        Node p = findIndex(index);
        return p.value;
    }
    //返回指定索引的节点
    private Node findIndex(int index) {
        int count = 0;
        Node p = head;
        while (count < index) {
            if (p == null) {
                throw new IllegalArgumentException();
            }
            p = p.next;
            count++;
        }
        return p;
    }
    //找到尾节点
    private Node findLast() {
        //如果是空链表, 压根找不到最后的节点
        if (head == null) {
            return null;
        }
        Node p;
        //当p.next == null时, 则p指向了尾节点
        for (p = head; p.next != null; p = p.next) {
            ;
        }
        return p;
    }
    //尾插法
    public void addLast(int value) {
        Node last = findLast();
        //如果是一个空链表
        if (last == null) {
            addHead(value);
            return;
        }
        last.next = new Node(value, null);
    }
    public void Insert(int index, int value) {
        //如果插入的是第一个节点
        if (index == 0) {
            addHead(value);
            return;
        }
        //先找到index位置处的前一个节点
        Node p = findIndex(index - 1);
        p.next = new Node(value, p.next);
    }
    //删除最后一个节点
    public void removeFrist() {
        //如果是空链表
        if (head == null) {
            throw new RuntimeException();
        }
        //找到头节点
        Node p = findIndex(0);
        head = p.next;
    }
    public void remove(int index) {
        //如果是空链表
        if (head == null) {
            throw new RuntimeException();
        }
        //如果要删除的是最后一个节点, 那么调用removeFrist()方法
        if (index == 0) {
            removeFrist();
            return;
        }
        Node p = findIndex(index - 1);
        p.next = p.next.next;
    }


    //使用迭代器进行遍历
    @Override
    public Iterator<Integer> iterator() {
        return new Iterator<Integer>() {
            Node q = head;
            @Override
            public boolean hasNext() {
                if (q != null) {
                    return true;
                }
                return false;
            }

            @Override
            public Integer next() {
                int value = q.value;
                q = q.next;
                return value;
            }
        };

    }
    //使用函数式接口进行链表的遍历
    public void Traverse2(Consumer<Integer> consumer) {
        for (Node p = head; p != null; p = p.next) {
            consumer.accept(p.value);
        }
    }

}

3. 注

  • foreach循环的底层使用了迭代器.所以如果一个类的对象想要使用foreach循环遍历,则其类必须实现Iterable接口,并重写其中的抽象方法(iterable()).在其return语句中实现了匿名内部类,重写了Iterator接口的两个重要的抽象方法,hasNext()和next().不断迭代将next函数返回的值赋值给临时变量element.

  • 在Traverse2方法中,我们使用了函数式接口来进行链表的遍历.

    复制代码
    public void Traverse2(Consumer<Integer> consumer) {
            for (Node p = head; p != null; p = p.next) {
                consumer.accept(p.value);
            }
        }
    
    @Test
        public void test3() {
            LinkListTest l = new LinkListTest();
            l.addLast(12);
            l.addLast(23);
            l.addLast(34);
            l.addLast(45);
            l.addLast(56);
            l.addLast(67);
            l.Traverse2(value -> {
                System.out.println("该节点的值为" + value);
            });
            l.Traverse2(System.out :: println);
        }

    形参是一个消费型的函数式接口,实参我们可以传入接口的实现类的对象(lambda表达式或者方法引用实现).

相关推荐
tg-zm8899962 小时前
2025返利商城源码/挂机自动收益可二开多语言/自定义返利比例/三级分销理财商城
java·mysql·php·laravel·1024程序员节
X***C8622 小时前
SpringBoot:几种常用的接口日期格式化方法
java·spring boot·后端
Promise4852 小时前
贝尔曼公式的迭代求解笔记
笔记·算法
前端达人2 小时前
你的App消息推送为什么石沉大海?看Service Worker源码我终于懂了
java·开发语言
小光学长2 小时前
基于ssm的宠物交易系统的设计与实现850mb48h(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
java·前端·数据库
编程大师哥2 小时前
vxe-table 透视表分组汇总及排序基础配置
java
fish_xk3 小时前
数据结构之二叉树中的堆
数据结构
8***84823 小时前
spring security 超详细使用教程(接入springboot、前后端分离)
java·spring boot·spring
9***J6283 小时前
Spring Boot项目集成Redisson 原始依赖与 Spring Boot Starter 的流程
java·spring boot·后端
M***Z2103 小时前
SQL 建表语句详解
java·数据库·sql