Java双向链表

2020-11-11  本文已影响0人  代江波

链表节点

package com.djb.doubleLinked;

public class DLinkedObject {

    public int id;
    public String name;
    public String nikeName;
    public DLinkedObject next;
    public DLinkedObject pre;

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

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

链表操作

package com.djb.doubleLinked;

public class DLinkedList {
    private DLinkedObject header = new DLinkedObject(0,"","");

    /**打印链表数据*/
    public void show(){
        DLinkedObject cur = header.next;
        while (cur != null){
            System.out.println(cur);
            cur = cur.next;
        }
        System.out.println();
    }

    /**在链表最后添加数据*/
    public void add(DLinkedObject object){
        DLinkedObject temp = header;
        while (temp.next != null){
            temp = temp.next;
        }
        temp.next = object;
        object.pre = temp;
    }

    /**链表添加数据后保持有序(根据DLinkedObject的id,id相同不可重复添加)*/
    public void addOrder(DLinkedObject object){
        DLinkedObject temp = header;

        while (true){
            if (temp.next == null){
                temp.next = object;
                object.pre = temp;
                break;
            }
            if (object.id < temp.next.id){
                object.next = temp.next;
                temp.next.pre = object;

                temp.next = object;
                object.pre = temp;
                break;
            }
            if (object.id == temp.next.id){
                System.out.println("该数据已经存在,不可重复添加");
                break;
            }
            temp = temp.next;
        }
    }

    /**根据索引添加数据,索引从0开始到链表数据总数为止*/
    public void add(int index, DLinkedObject object){
        if (index < 0 || index > count()){
            throw new IndexOutOfBoundsException("索引越界");
        }

        int curIndex = 0;
        DLinkedObject temp = header;
        DLinkedObject result = header;
        while (temp.next != null){
            if (temp.next.id == object.id){
                System.out.println("数据重复,无法添加");
                return ;
            }
            if (curIndex == index){
                result = temp;
            }

            curIndex++;
            temp = temp.next;
        }

        object.pre = result;
        if (result.next != null){
            result.next.pre = object;
        }

        object.next = result.next;
        result.next = object;
    }

    /**删除(根据要删除的数据的id)*/
    public void delete(DLinkedObject object){
        DLinkedObject cur = header.next;
        while (cur != null){
            if (cur.id == object.id){
                break;
            }
            cur = cur.next;
        }
        if (cur == null){
            System.out.println("列表中没有该数据,无法删除");
        }else {
            cur.pre.next = cur.next;
            if (cur.next != null){
                cur.next.pre = cur.pre;
            }
        }
    }

    /**删除索引位置的数据,索引从0开始到链表数据总数-1为止*/
    public void delete(int index){
        if (index < 0 || index > count() - 1){
            throw new IndexOutOfBoundsException("索引越界");
        }

        DLinkedObject temp = header;
        int currentIndex = 0;
        while (temp != null){
            if (currentIndex == index){
                break;
            }
            temp = temp.next;
            currentIndex++;
        }

        temp.next.pre = temp;
        temp.next = temp.next.next;
        
    }

    /**根据数据id更新数据*/
    public void update(DLinkedObject object){
        DLinkedObject cur = header.next;
        while (cur != null){
            if (cur.id == object.id){
                cur.name = object.name;
                cur.nikeName = object.nikeName;
                return;
            }
            cur = cur.next;
        }
        System.out.println("没有找到要修改的数据");
    }

    /**获取索引位置的数据,索引从0开始,到有效数据个数总和-1*/
    public DLinkedObject get(int index){
        if (index < 0 || index > count() - 1){
            throw new IndexOutOfBoundsException("索引越界");
        }
        DLinkedObject cur = header.next;
        int currentIndex = 0;
        while (cur != null){
            if (currentIndex == index){
                break;
            }
            cur = cur.next;
            currentIndex++;
        }
        return cur;
    }

    /**链表数据反转*/
    public void reversal(){
        DLinkedObject curNode = header.next;
        DLinkedObject nextNode = null;
        DLinkedObject newHeader = new DLinkedObject(0,"","");

        while (curNode != null){

            nextNode = curNode.next;
            curNode.pre = newHeader;
            if (nextNode != null){
                nextNode.pre = curNode;
            }

            curNode.next = newHeader.next;
            newHeader.next = curNode;
            curNode = nextNode;
        }

        header.next = newHeader.next;
    }

    /**链表有效数据个数*/
    public int count(){
        int count = 0;
        DLinkedObject cur = header.next;
        while (cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }
}

上一篇下一篇

猜你喜欢

热点阅读