Java集合之实现LinkedList

版权声明:本文为博主原创文章,转载请声明出处并添加原文链接。 https://blog.csdn.net/azsx02/article/details/83070052
package com.huhu.collection;

public class MyLinkedList {
    private Node first;
    private Node last;
    private Node n;
    private int size;

    public int size() {
        return size;
    }

    public void add(Object o) {
        // 添加到链表末端
        Node n = new Node();

        if (first == null) {
            n.obj = o;
            n.next = null;
            n.previous = null;

            first = n;
            last = first;
        } else {
            n.obj = o;
            n.previous = last;
            n.next = null;
            last.next = n;
            last = n;
        }
        size++;
    }

    public Object get(int index) {

        Node temp = new Node();
        temp = first;

        if (temp == null) {
            return null;
        } else if (index < 0 || index > size - 1) {
            throw new IndexOutOfBoundsException("index: " + index);
        } else {
            for (int i = 0; i < index; i++) {
                temp = temp.next;
            }
            return temp.obj;
        }

// while循环实现
//        int count = 0;
//        Node temp = first;
//
//        if (first == null) {
//            return null;
//        } else if (index > size-1){
//            throw new IndexOutOfBoundsException("index: " + index);
//        } else if (index == size -1) {
//            return last.obj;
//        } else {
//
//            while (temp.next != null) {
//                if (count == index) {
//                    return temp.obj;
//                } else {
//                    temp = temp.next;
//                    count++;
//                }
//            }
//            return null;
//        }
    }

    public Object remove(int index) {
        if (first == null) {
            return false;
        } else if (index < 0 || index > size-1) {
            throw new IndexOutOfBoundsException("index: " + index);
        } else {
            Node temp = first;
            for (int i=0; i<index; i++) {
                temp =temp.next;
            }
            unlink(temp);
            size--;
            return temp.obj;
        }
    }

    public boolean remove(Object o) {
        if (o == null) {
            for (Node x = first; x != null; x=x.next) {
                if (x == null) {
                    unlink(x);
                    return true;
                }
            }
        } else {
            for (Node x =first; x!=null; x=x.next) {
                if (o.equals(x.obj)) {
                    unlink(x);
                    return true;
                }
            }
        }
        return false;
    }

    public Object unlink(Node x) {
        final Object element = x.obj;
        final Node next = x.next;
        final Node prev = x.previous;

        if (prev == null) {
            first = next;
        } else {
           prev.next = next;
           x.previous = null; // 将删除的节点的数据置为null
        }

        if (next == null) {
            last = prev;
        } else {
            next.previous = prev;
            x.next = null; // 将删除的节点的数据置为null
        }

        x.obj = null; // 将删除的节点的数据置为null
        size--;
        return element;
    }


    public static void main(String[] args) {
        MyLinkedList myLinkedList = new MyLinkedList();
        myLinkedList.add("aaa");
        myLinkedList.add("bbb");
        System.out.println(myLinkedList.size);
        myLinkedList.remove(1);
        System.out.println(myLinkedList.size);
    }
}

class Node {
    Node previous;
    Node next;
    Object obj;

    public Node() {
    }

    public Node(Object obj){
        this.obj = obj;
    }
}

猜你喜欢

转载自blog.csdn.net/azsx02/article/details/83070052