简单的自定义 LinkedList

42 阅读1分钟
package thread.juc.linkedlist;


import java.util.NoSuchElementException;

/**
 * 简单的自定义 LinkedList
 */
public class LinkedList<E> {

    private Node<E> head = null;

    private int size;

    public LinkedList() {
        head = null;
    }


    class Node<E> {

        //Node<E> head;
        Node<E> next;
        E value;

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

        public Node() {
        }
    }


    /**
     * 这里我们默认添加到头部,也可以添加到尾部
     */
    public void addFirst(E e) {

        Node<E> node = new Node<E>(e);
        //添加进来的元素的地址指向head
        node.next = head;
        //head 保存的是当前添加进来的元素地址
        head = node;
        size++;
    }


    /**
     * 添加到尾部
     *
     * @param e
     */
    public void addLast(E e) {
        if (head == null) {
            head = new Node<>();
        }
        Node<E> node0 = head;
        // 新node
        Node<E> newNode = new Node<E>(e);
        if (node0 == null) {
            head = newNode;
        } else {
            node0.next = newNode;
        }

        size++;
    }


    /**
     * 删除某个元素
     * @param e
     * @return
     */
    public boolean remove(E e) {


        Node<E> current = head;

        //头
        if (current.value.equals(e)){
            head = current.next;
            return true;
        }

        Node<E> previous = null;
        while (current != null) {
            if (current.value.equals(e)) {
                previous.next = current.next;
                return true;
            }
            previous = current;
            current = current.next;
        }

        return false;

    }

    public Node<E> removeFirst() {

        if (size == 0) {
            throw new NoSuchElementException("linkList is empty ... ");
        }

        size--;

        //头部值
        Node<E> node = this.head;

        //头部指向下一个
        this.head = node.next;

        return node;
    }

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


    public String toString() {

        if (this.size == 0) {
            return "[]";
        }
        Node<E> node = head;

        StringBuilder builder = new StringBuilder("[");
        while (node != null) {
            System.out.println(node.value);
            builder.append(node.value).append(",");
            node = node.next;
        }

        String sb = builder.toString().substring(0, builder.length() - 1) + "]";

        return sb;
    }

    public int size() {
        return size;
    }

    public boolean contains(E e) {

        //第一个元素
        Node<E> head = this.head;

        while (head != null) {
            if (head.value.equals(e)) {
                return true;
            }
            head = head.next;
        }

        return false;
    }


    public static void main(String[] args) {

        LinkedList<String> list = new LinkedList<>();


        //todo  同时 添加到前后 没有做判断

        // 添加到前面
        list.addFirst("张三1");
        list.addFirst("张三2");
        list.addFirst("张三3");
        list.addFirst("张三4");
        list.addFirst("张三5");
        list.addFirst("张三6");


        // 添加到后面
        // list.addLast("张三7");
        // list.addLast("张三8");


        System.out.println(list.isEmpty());
        System.out.println(list.size());
        System.out.println(list.toString());
        System.out.println(list.contains("张三1"));
        System.out.println(" ============ rmove =========  ");
        list.remove("张三4");
        System.out.println(list.toString());
        list.remove("张三1");
        System.out.println(list.toString());

        list.remove("张三6");
        System.out.println(list.toString());


    }


}