1. 程式人生 > >Java集合之實現LinkedList

Java集合之實現LinkedList

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;
    }
}