JAVA-02 手写LinkedList

在数据结构的世界中,链表是一种非常基础且重要的数据结构。今天,我们将深入探讨一个自定义的链表实现 - CustomLinkedList 类。

一、链表的节点定义

在 CustomLinkedList 类中,首先定义了一个内部类 Node 来表示链表的节点。每个节点包含了数据 data 和指向下一个节点的引用 next 。通过构造函数 Node(Object data) ,可以方便地创建一个新的节点,并初始化数据和将 next 指针指向空。

class Node{
    Object data;
    Node next;

    public Node(Object data){
        this.data = data;
        this.next = null;
    }
}

二、链表的基本属性与构造函数

CustomLinkedList 类具有两个重要的属性:head 表示链表的头节点,size 记录链表中节点的数量。

构造函数 CustomLinkedList() 用于初始化一个空的链表,将 head 设置为 nullsize 设置为 0

private Node head;
private int size;

public CustomLinkedList(){
    head = null;
    size = 0;
}

三、链表的基本操作方法

1. 判空操作 isEmpty()

通过判断 size 是否为 0,来确定链表是否为空。

public boolean isEmpty(){
    return size == 0;
}

2. 获取链表大小 size()

返回链表中节点的数量。

public int size(){
    return size;
}

3. 新增节点 add(Object data)

用于向链表中添加新的节点。如果链表为空,将新节点设置为头节点;否则,遍历链表找到最后一个节点,将新节点添加到其后面。

public void add(Object data){
    Node newNode = new Node(data);
    if(head == null){
        head = newNode;
    }else {
        Node current = head;
        while(current.next!= null){
            current = current.next;
        }
        current.next = newNode;
    }
    size++;
}

4. 获取指定索引的节点数据 get(int index)

首先检查索引是否合法,如果索引超出范围,将抛出 IndexOutOfBoundsException 异常。然后通过遍历链表,找到指定索引的节点,并返回其数据。

public E get(int index){
    if (index < 0 || index >= size){
        throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
    }
    Node current = head;
    for(int i = 0; i < index; i++){
        current = current.next;
    }
    return (E) current.data;
}

5. 删除指定索引的节点 remove(int index)

同样先检查索引的合法性。如果要删除的是头节点,直接将 head 指向下一个节点;否则,找到要删除节点的前一个节点,将其 next 指针指向要删除节点的下一个节点。

public void remove(int index){
    if (index < 0 || index >= size){
        throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
    }
    if(index == 0){
        head = head.next;
    }else {
        Node current = head;
        for (int i = 0; i < index - 1; i++) {
            current = current.next;
        }
        current.next = current.next.next;
    }
    size--;
}

6. 清空链表 clear()

将 head 设置为 null,并将 size 重置为 0,实现链表的清空操作。

public void clear(){
    head = null;
    size = 0;
}

7. 打印链表 print()

通过遍历链表,将每个节点的数据打印出来,最后打印 null 表示链表结束。

public void print(){
    Node current = head;
    while(current!= null){
        System.out.println(current.data);
        current = current.next;
    }
    System.out.println("null");
}

四、示例演示

在 main 方法中,我们对自定义的链表进行了一系列的操作,包括添加元素、删除元素、获取元素、获取链表大小、清空链表和打印链表等,来验证我们的链表实现的正确性和功能。

public static void main(String[] args) {
    CustomLinkedList<Integer> list = new CustomLinkedList<Integer>();

    list.add(1);
    list.add(2);
    list.add(3);

    System.out.println("Initial LinkedList:");
    list.print(); // 打印初始链表

    list.remove(1); // 删除第二个元素
    System.out.println("\nAfter removing element at index 1:");
    list.print(); // 打印删除后的链表

    System.out.println("\nElement at index 1: " + list.get(1)); // 获取索引为 1 的元素
    System.out.println("Size of LinkedList: " + list.size()); // 获取链表大小

    list.clear(); // 清空链表
    System.out.println("\nAfter clearing the LinkedList:");
    list.print(); // 打印清空后的链表
}

 通过这段自定义链表的实现与示例演示,我们对链表的基本概念和操作有了更深入的理解和认识。希望这个自定义链表对您在数据结构的学习和应用中有所帮助!

四、可以直接运行的代码

ublic class CustomLinkedList<E> {
    class Node{
        //定义数据
        Object data;
        //定义next节点
        Node next;
        public Node(Object data){
            //构造函数
            this.data = data;
            //next指针指向空
            this.next = null;
        }
    }

    /**
     * 数据节点
     */
    private Node head;
    /**
     * 数据大小
     */
    private int size;

    /**
     * 构造函数
     */
    public CustomLinkedList(){
        head = null;
        size = 0;
    }

    /**
     * 判空
     * @return
     */
    public boolean isEmpty(){
        return size == 0;
    }

    /**
     * 获取大小
     * @return
     */
    public int size(){
        return size;
    }

    /**
     * 新增
     * @param data
     */
    public void add(Object data){
        //创建一个新的节点
        Node newNode = new Node(data);
        //如果是第一个元素
        if(head == null){
            //那么将newNode给第一个元素
            head = newNode;
        }else {
            //如果不是,读取head
            Node current = head;
            //循环链表,这也是它插入效率低的原因
            while(current.next != null){
                //寻找最后一个元素
                current = current.next;
            }
            //找到最后一个元素复制新的节点
            current.next = newNode;
        }
        size++;
    }
    public E get(int index){
        if (index < 0 || index >= size){
            throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
        }
        Node current = head;
        for(int i = 0; i < index; i++){
            current = current.next;
        }
        return (E) current.data;
    }

    /**
     * 删除
     * @param index
     */
    public void remove(int index){
        if (index < 0 || index >= size){
            throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
        }
        if(index == 0){
            head = head.next;
        }else {
            Node current = head;
            for (int i = 0; i < index - 1; i++) {
                current = current.next;
            }
            current.next = current.next.next;
        }
        size--;
    }

    /**
     * 清理
     */
    public void clear(){
        head = null;
        size = 0;
    }

    /**
     * 打印
     */
    public void print(){
        Node current = head;
        while(current != null){
            System.out.println(current.data);
            current = current.next;
        }
        System.out.println("null");
    }

    public static void main(String[] args) {
        CustomLinkedList<Integer> list = new CustomLinkedList<Integer>();

        list.add(1);
        list.add(2);
        list.add(3);

        System.out.println("Initial LinkedList:");
        list.print(); // 打印初始链表

        list.remove(1); // 删除第二个元素
        System.out.println("\nAfter removing element at index 1:");
        list.print(); // 打印删除后的链表

        System.out.println("\nElement at index 1: " + list.get(1)); // 获取索引为1的元素
        System.out.println("Size of LinkedList: " + list.size()); // 获取链表大小

        list.clear(); // 清空链表
        System.out.println("\nAfter clearing the LinkedList:");
        list.print(); // 打印清空后的链表
    }
}

   关注公众号:资小库,问题快速答疑解惑

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不会写爬虫的程序员B

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值