1. 程式人生 > >雙向連結串列設計

雙向連結串列設計

interface  ILink {
    /**
     * 連結串列增加節點操作
     * @param data 節點內容
     * @return
     */
    boolean add(Object data);

    /**
     * 判斷指定內容節點在連結串列中是否存在
     * @param data 要判斷的內容
     * @return 返回找到的節點索引
     */
    int contains(Object data);

    /**
     * 刪除指定內容節點
     * @param data
     * @return
     */
    boolean remove(Object data);

    /**
     * 根據指定下標修改節點內容
     * @param index 索引下標
     * @param newData 替換後的內容
     * @return 替換之前的節點內容
     */
    Object set(int index,Object newData);

    /**
     * 根據指定下標返回節點內容
     * @param index
     * @return
     */
    Object get(int index);

    /**
     * 連結串列清空
     */
    void clear();

    /**
     * 將連結串列轉為陣列
     * @return 返回所有節點內容
     */
    Object[] toArray();

    /**
     * 連結串列長度
     * @return
     */
    int size();

    /**
     * 遍歷連結串列
     */
    void printLink();
}

class ILinkImpl implements ILink {
    private Node head;
    private Node last;
    private int size;

    private class Node{
        private Node prev;
        private Object data;
        private Node next;
        private Node(Node prev,Object data,Node next) {
            this.prev = prev;
            this.data = data;
            this.next = next;
        }
    }
    @Override
    public boolean add(Object data) {
        Node temp = this.last;
        Node newNode = new Node(last,data,null);
        if(this.head == null){
            this.head = newNode;
        }else{
            temp.next = newNode;
        }
        this.last = newNode;
        this.size++;
        return true;
    }

    @Override
    public int contains(Object data) {
        Node temp = this.head;
        int i = 0;
        if(data == null){
            for(;temp != null;temp = temp.next){
                if(temp.data == null)
                    return i;
                i++;
            }
        }else{
            for(;temp != null;temp = temp.next){
                if(temp.data.equals(data))
                    return i;
                i++;
            }
        }
        return -1;
    }

    @Override
    public boolean remove(Object data) {
        Node temp = this.head;
        if(data == null){
            for(;temp != null;temp = temp.next){
                if(temp.data == null){
                    delete(temp);
                }
            }
        }else{
            for(;temp != null;temp = temp.next){
                if(temp.data.equals(data)) {
                    delete(temp);
                }
            }
        }
        this.size--;
        return true;
    }

    @Override
    public Object set(int index, Object newData) {
        if(!IsLinkIndex(index)){
            return null;
        }
        Node temp = findIndex(index);
        Object src_data = temp.data;
        temp.data = newData;
        return src_data;
    }

    @Override
    public Object get(int index) {
        if(!IsLinkIndex(index)){
            return null;
        }
        return findIndex(index).data;
    }

    @Override
    public void clear() {
        for(Node temp = this.head;temp != null;){
            Node node = temp.next;
            temp.data = null;
            temp = temp.prev = temp.next = null;
            temp = node;
            this.size--;
        }
    }

    @Override
    public Object[] toArray() {
        Object[] result = new Object[size];
        Node temp = this.head;
        int i = 0;
        for(;temp != null;temp = temp.next){
            result[i++] = temp.data;
        }
        return result;
    }

    @Override
    public int size() {
        return this.size;
    }

    @Override
    public void printLink() {
        Object[] data = this.toArray();
        for(Object temp : data){
            System.out.println(temp);
        }
    }

    private boolean IsLinkIndex(int index){
        return index >=0 && index < this.size;
    }

    private Node findIndex(int index){
        if(index <= (this.size>>1)){
            Node temp = this.head;
            for(int i = 0;i < index;i++){
                temp = temp.next;
            }
            return temp;
        }
        Node temp = this.last;
        for(int i = this.size-1;i > index;i--){
            temp = temp.prev;
        }
        return temp;
    }

    private void delete(Node node) {
        Node prev = node.prev;
        Node next = node.next;
        if(prev == null){
            this.head = node.next;
        }else{
            prev.next = next;
            node.prev = null;
        }
        if(next == null){
            this.last = prev;
        }else {
            next.prev = prev;
            node.next = null;
        }
        this.size--;
    }
}