自定義LinkedList

weixin_48264499發表於2020-12-25

語言學習

自定義LinkedList


/**
 * @Author:meihongsheng
 * @Description:com.mhs.test04
 */
public class TestLinkedList01<E> {
    //記錄頭結點和尾節點資訊
    private Node first;
    private Node last;

    /**
     * 記錄儲存的數量
     */
    private int size;

    /**
     * 增加元素的方法
     */
    public void add(E e){
        Node node = new Node(e);
        if(null == first){
            //起始位置的判斷
            first = node;
            last = node;
            size++;
        }else{
            //追加元素時,首先記錄新傳進來的元素頭指標last
            node.previous = last;
            //node.next = null;
            //然後設定原先last的尾指標是新傳進來的元素
            last.next = node;
            //將新傳進來的元素設定成last
            last = node;
            size++;
        }
    }

    /**
     * 根據指定索引位置增加元素
     * @param index
     * @param e
     */
    public void add(int index,E e){
        Node node = new Node(e);
        checkIndex(index);
        Node nodeIndex = getNode(index);
        if(nodeIndex != null){
            Node up = nodeIndex.previous;
            if(up != null){
                up.next = node;
                nodeIndex.previous = node;
                node.next = nodeIndex;
                node.previous = up;
            }
            if(index == 0){
                first.previous = node;
                node.next = first;
                first = node;
            }
        }
        size++;
    }

    /**
     * 根據索引獲取元素
     * @param index
     * @return
     */
    public Object get(int index) {
        checkIndex(index);
        Node temp = first;
        if (index <= (size >> 1)) {
            for (int i = 0; i < index; i++) {
                temp = temp.next;
            }
        } else {
            temp = last;
            for (int i = size - 1; i > index; i--) {
                temp = temp.previous;
            }
        }
        return temp != null ? temp.element : null;
    }

    /**
     * 根據索引值刪除指定元素
     * @param index
     * @return
     */
    public void remove(int index) {
        checkIndex(index);
        Node node = getNode(index);
        if (node != null) {
            Node up = node.previous;
            Node down = node.next;
            if (up != null) {
                up.next = down;
            }
            if (down != null) {
                down.previous = up;
            }
            if(index==0){
                first = down;
            }
            if(index==size-1){
                last = up;
            }

        }
        size--;
    }

    /**
     * 獲取節點資訊
     * @param index
     * @return
     */
    public Node getNode(int index){
        checkIndex(index);
        Node temp = first;
        if(index <= (size>>1)){
            for (int i = 0; i < index; i++) {
                temp = temp.next;
            }
        }else{
            temp = last;
            for (int i = size - 1; i > index; i--) {
                temp = temp.previous;
            }
        }
        return temp;
    }
    /**
     * 列印資訊
     * @return
     */
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder("[");
        Node temp = first;
        if(temp == null){
            sb.append("]");
        }else{
            while(temp != null){
                //判斷是否是最後一個元素
                if(temp.next == null){
                    sb.append(temp.element + "]");
                }else{
                    sb.append(temp.element + ",");
                }
                temp = temp.next;
            }
        }
        return sb.toString();
    }

    /**
     * 判斷索引是否超出範圍
     * @param index
     */
    public void checkIndex(int index){
        if(index < 0 || index > size - 1){
            throw new RuntimeException("索引超出範圍");
        }
    }

    public static void main(String[] args) throws Exception {
        TestLinkedList01<String> ts = TestLinkedList01.class.getConstructor().newInstance();
        ts.add("a");
        ts.add("b");
        ts.add("c");
        ts.add("d");
        ts.add("e");
        ts.add(0,"f");
        System.out.println(ts);
        System.out.println(ts.last.element);
        System.out.println(ts.first.element);
//        System.out.println(ts.getNode(4).element);
    }
}
/**
 * 定義linkedList節點資訊
 * @Author:meihongsheng
 * @Description:com.mhs.test04
 */
public class Node {
    Node previous;//頭指標
    Node next;//尾指標
    Object element;//元素資訊

    public Node(Node previous, Node next, Object element) {
        this.previous = previous;
        this.next = next;
        this.element = element;
    }

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

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

相關文章