java 单链表简单实现

为了方便学习写的简单的链表,方便学习算法与数据结构的同学。
不多解释,直接看代码:

public class Link_List {

    private node head = null;
    private node end = null;
    private int size;

    private class node{
        public T value;//值
        node next = null;//下一个节点
        public node() {

        }
        public node(T value) {
            this.value = value;
        }
        @Override
        public String toString() {
            return this.value.toString();
        }
        public boolean equals(node nod) {
            if(this.value.equals(nod.value)) {
                return true;
            }
            return false;
        }
    }
    //构造方法
    public Link_List(T[] arr) {
        this.size = arr.length;
        head = new node();
        node nod = head;
        for(int i = 0; i < arr.length ; i++) {
            nod.value = arr[i];
            nod.next = new node();
            end = nod;//更新末尾节点,待优化
            nod = nod.next;
        }
    }
    public Link_List(T value) {
        head = new node();
        head.value = value;
        size = 1;
        end = head;
    }
    public Link_List() {

    }

    private node findNode(int index) {
        node nod = head;
        for(int i = 0 ; i < index ; i++ ) {
            nod = nod.next;
        }
        return nod;
    }

    public void add(T value) {
        node nod = findNode(size-1);
        nod.next = new node(value);
        end = nod.next;
        size++;
    }
    public void add(T value , int index) {
        if(index == size-1) {
            add(value);
        }else if(index == 0){
            node nod = new node(value);
            nod.next = head;
            head = nod;
            size++;
        }else {
            node preNode = findNode(index - 1);
            node nextNode = findNode(index);
            node nod = new node(value);
            preNode.next = nod;
            nod.next = nextNode;
            size++ ;
        }
    }

    public void delete(int index) {
        if(index == size-1) {
            end = null;
            end = findNode(index-1);
            size--;
        }else if(index == 0){
            head = head.next;
            size--;
        }else {
            node preNode = findNode(index - 1);
            node nextNode = findNode(index + 1);
            preNode.next = nextNode;
            findNode(index).next = null;//释放持有的引用
            size--;
        }
    }

    public T get(int index) {
        node nod = findNode(index);
        return nod.value;
    }

    @Override
    public String toString() {
        node nod = head;
        String res = head.toString() ;
        for(int i = 0 ; i < size ; i++) {
            nod = nod.next;
            if(nod != null) {
                res += " >> " + nod.value;
            }else {
                res += " >> null";
            }
        }
        return res;
    }

    public void reverse() {
        node nod = head;
        node pre = new node();
        while(nod != null){
            node next = nod.next;
            nod.next = pre;
            pre = nod;
            nod = next;
        }
        end = head;
        head = pre;
    }
}

测试:

public static void main(String[] args) {
        //test:
        Integer[] test = new Integer[] {0,1,2,3,4,5,6,7,8,9};
        Link_List mLink_List = new Link_List(test);
        System.out.println(mLink_List);
        mLink_List.delete(2);
        mLink_List.add(666,2);
        System.out.println(mLink_List.get(2));
        System.out.println(mLink_List);
    }

结果:

Console:

0 >> 1 >> 2 >> 3 >> 4 >> 5 >> 6 >> 7 >> 8 >> 9 >> null
666
0 >> 1 >> 666 >> 3 >> 4 >> 5 >> 6 >> 7 >> 8 >> 9 >> null

你可能感兴趣的:(算法学习)