Java環(huán)形單向鏈表

鏈表節(jié)點

package com.djb.circleLinked;

public class CLinkedObject {
    public int id;
    public String name;
    public CLinkedObject next;

    public CLinkedObject(int id, String name){
        this.id = id;
        this.name = name;
    }

    @Override
    public String toString() {
        return "CLinkedObjct{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

鏈表操作

package com.djb.circleLinked;

public class CLinkedList {

    public CLinkedObject firstObject = null;

    public void show(){
        if (firstObject != null){
            CLinkedObject cur = firstObject;
            while (true){
                System.out.println(cur);

                //當(dāng)只有一個元素是,該元素的next指向他自己
                if (cur.next == firstObject || cur.next == cur){
                    break;
                }
                cur = cur.next;
            }
        }else {
            System.out.println("鏈表為空");
        }
    }

    /**
     * 添加一個元素,元素id有重復(fù)的不可添加
     * @param object 要添加的元素
     */
    public void add(CLinkedObject object){
        if (object == null){
            throw new NullPointerException("傳入的元素為空");
        }

        if (firstObject == null){
            firstObject = object;
            firstObject.next = object;
        }else {
            CLinkedObject cur = firstObject;
            while (true){
                if (cur.id == object.id){
                    System.out.println("數(shù)據(jù)重復(fù),不可添加");
                    return;
                }

                if (cur.next == firstObject){
                    break;
                }
                cur = cur.next;
            }
            cur.next = object;
            object.next = firstObject;
        }
    }

    /**
     * 按照索引添加元素,由于是圓形鏈表,當(dāng)index為0時,因為firstObject已經(jīng)固定,所以顯示的為最后一個
     * @param index 元素索引
     * @param object 要添加的元素
     */
    public void add(int index, CLinkedObject object){
        if (index < 0 || index > count()){
            throw new IndexOutOfBoundsException("傳入的索引越界");
        }

        if (object == null){
            throw new NullPointerException("傳入的元素為空");
        }

        CLinkedObject cur = firstObject;
        CLinkedObject pre = null;
        int currentIndex = 0;
        while (true){
            if (cur.id == object.id){
                System.out.println("數(shù)據(jù)重復(fù),無法添加");
                return;
            }

            //找到要加入的位置的前一位
            if (index - 1 == currentIndex){
                pre = cur;
            }

            if (cur.next == firstObject){
                //如果沒有找到,說明傳入的index為0,這時前一位就是列表的最后一位
                if (pre == null){
                    pre = cur;
                }
                break;
            }

            cur = cur.next;
            currentIndex++;
        }
        object.next = pre.next;
        pre.next = object;
    }


    public void delate(CLinkedObject object){
        if (firstObject == null){
            System.out.println("鏈表為空");
        }
        CLinkedObject cur = firstObject;
        while (true){
            if (cur.next.id == object.id){
                if (object == firstObject){
                    cur.next = cur.next.next;
                    firstObject = cur.next;
                    break;
                }else {
                    cur.next = cur.next.next;
                    break;
                }
            }

            if (cur.next == firstObject){
                System.out.println("沒有找到該元素,刪除失敗");
                break;
            }
            cur = cur.next;
        }
    }


    /**
     * 獲取鏈表的元素個數(shù)
     */
    public int count(){
        if (firstObject != null){
            CLinkedObject cur = firstObject;
            int count = 0;
            while (true){
                count++;
                cur = cur.next;
                if (cur == firstObject){
                    break;
                }
            }
            return count;
        }else {
            return 0;
        }
    }

    /**
     * 獲取指定位置的元素
     * @param index 鏈表的索引,從0開始到所有元素的總數(shù)-1
     */
    public CLinkedObject get(int index){
        if (index < 0 || index > count() - 1){
            throw new IndexOutOfBoundsException("索引越界");
        }
        CLinkedObject cur = firstObject;
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }
        return cur;
    }

    /**
     * 從fromIndex開始,每次間隔interval依次取出鏈表的元素
     * @param fromIndex 開始的位置,從0開始到該鏈表的元素的總數(shù)-1
     * @param interval 每次跳過的個數(shù),該數(shù)不得小于1和大于該鏈表的元素的總數(shù)-1
     */
    public void pop(int fromIndex, int interval){
        if (fromIndex < 0 || fromIndex > count() - 1){
            throw new IndexOutOfBoundsException("傳入的跳轉(zhuǎn)數(shù)越界");
        }

        if (interval < 1 || interval > count() - 1){
            throw new IndexOutOfBoundsException("傳入的跳轉(zhuǎn)數(shù)越界");
        }


        CLinkedObject pre = fromIndex == 0 ? get(count() - 1) : get(fromIndex - 1);

        while (pre != pre.next){

            System.out.println(pre.next + "出鏈表");

            if (pre.next == firstObject){//因為firstObject由該類的對象引用著,所以只能手動銷毀
                pre.next = pre.next.next;
                firstObject = null;
            }else {
                pre.next = pre.next.next;
            }

            for (int i = 1 ; i < interval; i ++){
                pre = pre.next;
            }
        }
        //因為最后剩下的一個元素的next指向他自己,只有當(dāng)此方法結(jié)束后才會銷毀
        System.out.println(pre.next + "出鏈表");
        //當(dāng)最后剩下的是firstObject元素時,由于有該類的對象引用著,所以要手動銷毀
        if (pre.next == firstObject){
            firstObject = null;
        }

    }

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容