用Java泛型实现链表

该博客介绍了链表的基本操作,包括增删查、按位置和值删除节点,并实现链表的原地反转。通过示例展示了如何创建一个链表,进行一系列操作并打印结果。此外,还提供了单元测试来验证链表操作的正确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

主要实现了增删查以及翻转。改比较简单就没写。把前面的组合一下就可以做出来了。
自测没有问题。如果发现存在BUG,请指正。

package linklist;

import java.util.Objects;
import java.util.Scanner;

/**
 * 节点类,用于定义节点的内容
 *
 * @param <T> 使用时指定类型,注意类型不可以为基本类型。
 * @date 2020-12-16 10:45
 */
class Node<T> {
    T value;
    Node<T> next;

    public Node(T value, Node<T> next) {
        this.value = value;
        this.next = next;
    }
}

/**
 * 链表类,用于实现链表的相关操作
 *
 * @author SQH
 */
class LinkList<T> {
    private int length = 0;
    private Node<T> head;

    public LinkList() {
        head = new Node<>(null, null);
    }


    public boolean isEmpty() {
        return head.next == null;
    }

    public Node<T> getNodeByLocation(int location) {
        if (location < 1) {
            throw new RuntimeException("数字错误,位置不可以小于1\n");
        }

        Node<T> p = head.next;
        int count = 1;
        while (p != null && count != location) {
            count++;
            p = p.next;
        }

        return p;
    }

    public Node<T> getNodeByNum(T i) {
        Node<T> p = head.next;
        while (p != null && p.value != i) {
            p = p.next;
        }

        return p;
    }

    public void addNodeFront(T value) {
        Node<T> p = head;
        Node<T> newNode = new Node<>(value, null);
        newNode.next = p.next;
        p.next = newNode;
        length++;
    }

    public void addNodeBack(T value) {
        Node<T> p = head;
        while (p.next != null) {
            p = p.next;
        }
        p.next = new Node<>(value, null);
        length++;
    }

    public void removeByNum(T value) {
        Node<T> p = head;
        while (p.next != null && p.next.value != value) {
            p = p.next;
        }
        if (p.next != null) {
            p.next = p.next.next;
            length--;
        }
    }

    public void removeByLocation(int location) {
        if (location > length) {
            return;
        }

        Node<T> p = head;
        for (int i = 0; i < location - 1 && p.next != null; i++) {
            p = p.next;
        }

        if (p.next != null) {
            p.next = p.next.next;
            length--;
        }
    }

    public int getLength() {
        return length;
    }

    public void show() {
        Node<T> p = head.next;
        while (p != null) {
            System.out.print(p.value + "\t");
            p = p.next;
        }
        System.out.println();
    }

    public void destroy() {
        head.next = null;
        length = 0;
    }

    /**
     * 自身原地反转,不返回额外的结点
     */
    public void reverse() {
        Node<T> pre = null;
        Node<T> current = head.next;
        while (current != null) {
            Node<T> temp = current.next;
            current.next = pre;
            pre = current;
            current = temp;
        }
        head = new Node<>(null, pre);
    }
}

/**
 * @author SQH
 */
public class LinkListTest {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        LinkList<Integer> head = new LinkList<>();

        for (int i = 0; i < 20; i++) {
            head.addNodeFront(i);
        }

        System.out.println("isEmpty: " + head.isEmpty());
        System.out.println("Original list: ");
        head.show();
        System.out.println("Current length: " + head.getLength() + "\n");

        head.removeByLocation(5);
        System.out.println("After removing node: ");
        head.show();
        System.out.println("Current length: " + head.getLength() + "\n");

        head.removeByNum(9);
        System.out.println("After removing node: ");
        head.show();
        System.out.println("Current length: " + head.getLength() + "\n");

        head.addNodeBack(4);
        System.out.println("After adding node: ");
        head.show();
        System.out.println("Current length: " + head.getLength() + "\n");

        System.out.print("input the number you want to find: ");
        Node<Integer> node = head.getNodeByNum(in.nextInt());
        System.out.println(Objects.requireNonNullElse(node, "No matched node!") + "\n");

        try {
            System.out.print("input the node location you want to find: ");
            Node<Integer> locationNode = head.getNodeByLocation(in.nextInt());
            System.out.println(Objects.requireNonNullElse(locationNode, "No matched node!") + "\n");
        } catch (RuntimeException e) {
            System.out.println(e.getMessage());
        }

        System.out.print("before reverse: ");
        head.show();
        System.out.print("after  reverse: ");
        head.reverse();
        head.show();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

随风舞落叶殇

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

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

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

打赏作者

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

抵扣说明:

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

余额充值