链表实现-纯属自嗨

package DataStructure.LinerList;

import java.util.NoSuchElementException;
import java.util.Objects;

public class LinkedList<T> {

    private Node<T> head;

    public LinkedList() {
        head = new Node<>();
    }

    public LinkedList(T values[]) {
        this();//空链表构造
        Objects.requireNonNull(values);//判空
        if (values.length > 0) {//长度大于0
            Node node = this.head;
            for (int i = 0 ; i < values.length ; i ++) {
                node.next = new Node(values[i], node.next);
                node = node.next;
            }
        }
    }

    public boolean isEmpty() {//是否为空
        return this.head.next == null;
    }

    public T get(int i) {//获得指定位置的data
        int nowCount = 0;/*从0开始*/
        Node now = this.head;
        for ( ; now.next != null/*如果是空表的话,进不去*/ ; nowCount ++) {
            now = now.next;//和nowCount一样从0开始
            if (i == nowCount) {
                return (T)now.data;
            }
        }
        return null;
    }

    public void set(int i, T x) {/*设置指定位置的值*/
        int nowCount = 0;
        Node now = this.head;
        for ( ; now.next != null/*now是最后一个,已经判断过*/ ; nowCount ++) {
            now = now.next;/*和nowCount一样从0开始*/
            if (i == nowCount) {
                now.data = x;
            }
        }
    }

    public int size() {/*大小*/
        int nowCount = 0;
        Node now = this.head;
        for ( ; now.next != null/*最后一个,已经加入计数*/ ; nowCount ++) {
            now = now.next;/*从第0个开始*/
            /*当最后一个的时候,nowCount原本是最后一个的下标,在循环最后+1*/
        }
        return nowCount;
    }

    public String toString() {
        StringBuilder stringBuilder = new StringBuilder("[");//创建结果
        Node now = this.head;//第 “-1” 个
        if (now.next == null) {//空表
            stringBuilder.append("]");
            return stringBuilder.toString();
        }
        for ( ; now.next.next != null/*倒数第二个节点*/ ; ) {/*因为now.next != null,所以没有空指针的可能性*/
            now = now.next;//第一次循环从头结点到第一个节点
            //最后一次循环从倒数第三个节点到倒数第二个节点
            stringBuilder.append(now.data.toString() + ", ");
        }
        now = now.next;//倒数第一个节点
        stringBuilder.append(now.data.toString() + "]");//添加
        return stringBuilder.toString();
    }

    /*分析:我们能找到第i个节点,但是我们需要的不是第i个节点
    * 我们需要第 i-1 个节点,将第 i-1 个节点的后一个设置成我们需要的节点*/
    public Node<T> insert(int index, T x) {
        Node pre = this.head;/*头结点开始*/
        int count = -1;
        for ( ; pre.next != null ; ) {
            /*pre最多支持到最后一个,即第 size-1 个
            * count此时是size*/
            pre = pre.next;/*pre从0开始,而count从-1开始*/
            if (index == count) {
                pre.next = new Node(x, pre.next);
                break;
            }
        }
        return pre.next;//如果添加失败就是null(就算pre是最后一个,即循环完毕的情况,pre.next就是null)
    }

    public Node<T> insert(T x) {/*尾插*/
        Node pre = this.head;
        for ( ; pre.next != null/*pre是最后一个跳出循环*/ ; ) {
            pre = pre.next;
        }
        pre.next = new Node(x, pre.next);
        return pre.next;
    }

    public Node<T> remove(int i) {/*移除,还是需要第 i 个节点的前一个节点*/
        int count = -1;
        Node pre = this.head;
        Node temp = null;
        if(this.head.next == null) {//若表空,抛出异常
            throw new NoSuchElementException();
        }
        for ( ; pre.next.next != null ; ) {/*之后检查的情况,如果空表的话将报错*/
            /*我们需要pre最大倒数第二个,i最大是size - 1*/
            pre = pre.next;/*pre从0开始,count从-1开始*/
            if (count == i) {
                temp = pre.next;
                pre.next = pre.next.next;
            }
        }
        return temp;
    }

    public void clear() {
        this.head.next = null;
    }

    public int search(T key) {/*查询一个指定值首次出现的位置*/
        int result = 0;
        Node node = this.head;
        if (key == null) {
            for ( ; node.next != null ; result ++ ) {
                /*最后一个时跳出,跳出前,最后一个经过检验*/
                node = node.next;/*从0开始*/
                if (node.data == null) {
                    return result;
                }
            }
        } else  {
            for ( ; node.next != null ; result ++ ) {
                /*最后一个时跳出,跳出前,最后一个经过检验*/
                node = node.next;/*从0开始*/
                if (key.equals(node.data)) {
                    return result;
                }
            }
        }
        return -1;//查询不到
    }

    public boolean contains(T key) {
        return search(key) != -1;
    }

    public Node<T> insertDifferent(T x) {
        Node node = this.head;
        boolean flag = true;
        if (x == null) {
            for ( ; node.next != null ; ) {
                /*node到最后一个结束,最后一个已经判断*/
                node = node.next;/*从0开始*/
                if (node.data == null) {
                    flag = false;
                    break;
                }
            }
        } else {
            for ( ; node.next != null ; ) {
                /*node到最后一个结束,最后一个已经判断*/
                node = node.next;/*从0开始*/
                if (x.equals(node.data)) {
                    flag = false;
                    break;
                }
            }
        }
        while (node.next != null) {
            node = node.next;
        }
        /*跳出循环时,node是最后一个*/
        if (flag) {
            node.next = new Node(x, node.next);
        }
        return node.next;
    }

    public T remove(T key) {
        boolean flag = true;
        T temp;
        if (this.head.next == null) {//空表
            throw new NoSuchElementException();
        }
        if (key == null) {
            for (Node node = this.head ; node.next.next != null ; ) {
                if (node.next.data == null) {
                    temp = (T)node.next.data;
                    node.next = node.next.next;
                    return temp;
                }
            }
        } else {
            for (Node node = this.head ; node.next.next != null ; ) {
                if (key.equals(node.next.data)) {
                    temp = (T)node.next.data;
                    node.next = node.next.next;
                    return temp;
                }
            }
        }
        throw new NoSuchElementException();
    }

}

class Node<T> {
    public T data;
    public Node next;

    public Node() {
    }

    public Node(T data, Node next) {
        this.data = data;
        this.next = next;
    }
}

猜你喜欢

转载自blog.csdn.net/jdie00/article/details/81393773