Java环形单向链表

链表节点

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

                //当只有一个元素是,该元素的next指向他自己
                if (cur.next == firstObject || cur.next == cur){
                    break;
                }
                cur = cur.next;
            }
        }else {
            System.out.println("链表为空");
        }
    }

    /**
     * 添加一个元素,元素id有重复的不可添加
     * @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("数据重复,不可添加");
                    return;
                }

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

    /**
     * 按照索引添加元素,由于是圆形链表,当index为0时,因为firstObject已经固定,所以显示的为最后一个
     * @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("数据重复,无法添加");
                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;
        }
    }


    /**
     * 获取链表的元素个数
     */
    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开始到所有元素的总数-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开始到该链表的元素的总数-1
     * @param interval 每次跳过的个数,该数不得小于1和大于该链表的元素的总数-1
     */
    public void pop(int fromIndex, int interval){
        if (fromIndex < 0 || fromIndex > count() - 1){
            throw new IndexOutOfBoundsException("传入的跳转数越界");
        }

        if (interval < 1 || interval > count() - 1){
            throw new IndexOutOfBoundsException("传入的跳转数越界");
        }


        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指向他自己,只有当此方法结束后才会销毁
        System.out.println(pre.next + "出链表");
        //当最后剩下的是firstObject元素时,由于有该类的对象引用着,所以要手动销毁
        if (pre.next == firstObject){
            firstObject = null;
        }

    }

}

你可能感兴趣的:(Java环形单向链表)