单链表增删改查

节点类

class Node{
    public int id;
    public String name;
    public Node next;    //指向下一个节点

    public Node() {
    }

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

链表类

class SingleLinkedList{
    //单链表对象
    private Node head;   //头节点

    public SingleLinkedList() {
    }

    public SingleLinkedList(Node head) {
        this.head = head;
    }
}

下面的两个插入方法是SingleLinkedList里面的方法

插入到最后

//单链表添加节点到最后的方法的方法
    public void add(Node node){
        //需要一个中间变量来遍历
        Node cur = head;
        //走到最后
        while(true){
            if(cur.next == null){
                //找到最后一个节点了
                break;
            }else{
                cur =cur.next;
            }
        }
        //插入即可
        cur.next = node;
    }

按顺序插入(完成后,链表有序)

当遇到节点比插入的节点的值大的时候,说明要插入的地方就是遇到的节点的前面!
这里的代码不能设置了不能插入已有数据!

//插入数据的时候同时排好序
    public void addByOrder(Node node){
        Node cur = head;
        while(true){
            if(cur.next != null){
                if(cur.next.id > node.id){
                    //说明找到要插入的地方了
                    break;
                }else if(cur.next.id == node.id){
                    //存在了该节点,那么就提示用户不能插入
                    System.out.println("已存在该节点不能进行删除");
                    return;
                }else{
                    cur = cur.next;
                }
            }else{
                break;
            }
        }
        //找到了要插入的位置,直接插入
        node.next = cur.next;
        cur.next = node;  //无缝衔接
    }

更新节点

//修改单链表
    public void update(Node node){
        Node cur = head;
        while(true){
            //找到要被修改的节点
            if(cur.next != null){
                if(cur.next.id == node.id){
                    cur.next.name = node.name;
                    break;
                }
                cur = cur.next;
            }else{
                //找到了末尾都没找到
                System.out.println("没有该节点");
                return;
            }
        }
    }

删除节点

思路:找到要删除的节点的前置节点,然后让前置节点越过要删除的节点指向后一个节点。那么就删除成功了。没有引用指向的节点会被垃圾回收机制收回。

//删除节点
    public void delete(int id){
        Node cur = head;
        //需要找到要删除的节点的前置节点
        while(true){
            if(cur.next != null){
                if(cur.next.id == id){
                    break;//找到了直接跳出
                }
                cur = cur.next;
            }else{
                //找到末尾也没有找到
                System.out.println("没有找到要删除的节点");
                return;
            }
        }
        //走到这里说明找到了可以更新,跳过要删除的节点直接指向后一个,让GC回收
        cur.next = cur.next.next;
    }

单链表反转

思路:我们新创建一个节点,然后遍历原链表,不断的将遍历到的元素头插到新链表中。最后将新链表就是原链表的反转 !

//反转单链表
    public void reverse(Node head){
        Node newHead = new Node();
        Node cur = head.next;   //循环遍历用到
        Node next = null;       //记录cur的下一个节点,保证改变链表引用后链表不会断掉
        while(cur != null){
            next = cur.next;
            cur.next = newHead.next;  //让当前节点指向newHead的下一个
            newHead.next = cur;
            cur = next;               //遍历下一个节点
        }
        head.next = newHead.next;
    }

如发现错误请评论一哈,马上改

发布了57 篇原创文章 · 获赞 11 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_42419462/article/details/104729520