LinkedList<E>

这个博客展示了如何从头开始实现一个简单的Java LinkedList。它包括添加元素(addFirst)、检查元素存在(contains)、移除元素(removeFirst)以及转换为字符串的方法。示例代码还演示了如何使用这个LinkedList实例进行操作。
摘要由CSDN通过智能技术生成

package com.thread.utils.collections;


public class LinkedList<E> {
    private int size;
    private Node<E> first;

    private final String PLAIN_NULL = "null";
    private final Node<E> NULL = (Node<E>) null;

    public LinkedList() {
        this.first = NULL;
    }

    public int size() {
        return size;
    }

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

    public void addFirst(E e) {
        Node<E> node = new Node<>(e);
        node.next = first;
        first = node;
        size++;
    }

    public static <E> LinkedList<E> of(E... elements) {
        LinkedList<E> list = new LinkedList<>();
        if (elements.length > 0) {
            for (E e : elements) {
                list.addFirst(e);
            }
        }
        return list;
    }

    public boolean contains(E e) {
        Node<E> current = first;
        while (current != null) {
            if (current.value == e) {
                return true;
            }
            current = current.next;
        }
        return false;
    }

    public E removeFirst() {
        if (this.isEmpty()) throw new RuntimeException("no such elements");
        Node<E> node = first;
        first = node.next;
        size--;
        return node.value;
    }

    public LinkedList<E> addLast(E e) {
        Node<E> node = new Node<>(e);
        node.next = NULL;
        return this;
    }

    private static class Node<E> {
        E value;
        Node<E> next;

        public Node(E value) {
            this.value = value;
        }

        @Override
        public String toString() {
            if (null != value)
                return value.toString();
            return "null";
        }
    }

    @Override
    public String toString() {
        if (this.isEmpty()) return "[]";
        else {
            StringBuilder builder = new StringBuilder("[");
            Node<E> node = first;
            while (node != null) {
                builder.append(node.toString()).append(",");
                node = node.next;
            }
            builder.replace(builder.length() - 1, builder.length(), "]");
            return builder.toString();
        }
    }

    public static void main(String[] args) {
        LinkedList<String> list = LinkedList.of("Hello", "World", "Scala", "Java", "Thread");
        list.addFirst("Concurrentcy");
        System.out.println(list.size);
        System.out.println(list.contains("Java"));
        System.out.println(list);
        while (!list.isEmpty()) {
            System.out.println(list.removeFirst());
        }
        System.out.println(list.size);
        System.out.println(list.isEmpty());
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值