Single chain CRUD

Node Class

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

Objectlist

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

    public SingleLinkedList() {
    }

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

The following two methods are inserted inside method SingleLinkedList

Inserted in the last

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

Sequentially inserted (completed, ordered list)

When faced with a large value of the node than the node insertion, indicating that the node to be inserted in front of the place is encountered!
The code here can not be inserted into the existing data set!

//插入数据的时候同时排好序
    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;  //无缝衔接
    }

Updates 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;
            }
        }
    }

Delete Node

Ideas: Find the front node to be deleted, and then let the front node to be deleted after crossing points to one of the nodes. Then delete succeeded. No references to the node will be resumed garbage collection.

//删除节点
    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;
    }

Singly linked list reversal

Idea: we create a new node, and then traverse the original list, we will continue to traverse the elements head into the new list. Finally, the new list is the original list of reversal!

//反转单链表
    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;
    }

If mistakes are found please comment Kazakhstan, immediately change

Published 57 original articles · won praise 11 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_42419462/article/details/104729520