[Xiaobai Learning Algorithm] 6. Modification and deletion of singly linked list

Continue to look at the singly linked list from the previous chapter.
Previously, the singly linked list was traversed and inserted. This chapter continues to use code to modify and delete.

1. Modification of singly linked list

Free coupon m.cps3.cn

To modify the node information, you first need to find the corresponding node, and then the code in the previous chapter, that is, the hero's rank no cannot be modified, and it must be used to find the node.
Other information can be moved.

In addition, consider the situation that the node to be modified cannot be found in the singly linked list. The code here is not all posted, now continue to add methods to modify node information in the SingleLinkedListclass
:

    // 修改结点信息
    // 根据结点的no来修改即可
    public void  update(HeroNode newHeroNode) {
        // 判断链表是否为空
        if (headNode.next == null) {
            System.out.println("链表为空");
            return;
        }
        // 借助辅助变量temp
        HeroNode temp = headNode.next;
        boolean flag = false; // 表示是否可以找到要修改的结点
        while (true) {
            if (temp == null) {
                break; // 遍历结束
            }
            if (temp.no == newHeroNode.no) {
                // 找到了对应的结点
                flag = true;
                break;
            }
            temp = temp.next;
        }
        // 根据flag 判断是否已经找到对应要修改的结点
        if (flag) {
            temp.name = newHeroNode.name;
            temp.nickname = newHeroNode.nickname;
        } else {
            System.out.printf("未找到编号%d的结点,不可修改
", newHeroNode.no);
        }
    }

Modify the test code in the main method to see if the result meets expectations.

    public static void main(String[] args) {
        // 测试
        HeroNode hero1 = new HeroNode(1, "易大师","无极剑圣");
        HeroNode hero2 = new HeroNode(2, "李青","盲僧");
        HeroNode hero3 = new HeroNode(3, "艾希","寒冰射手");
        HeroNode hero4 = new HeroNode(4, "菲奥娜","无双剑姬");

        // 创建链表
        SingleLinkedList singleLinkedList = new SingleLinkedList();
        // 加入对象结点
        singleLinkedList.addByNo(hero1);
        singleLinkedList.addByNo(hero4);
        singleLinkedList.addByNo(hero2);
        singleLinkedList.addByNo(hero3);
        // 显示链表内容
        singleLinkedList.linkList();

        // 测试修改
        HeroNode hero5 = new HeroNode(2, "李青","瞎子");
        singleLinkedList.update(hero5);
        System.out.println("修改后的链表:");
        singleLinkedList.linkList();
    }

operation result:

HeroNode{no=1, name='易大师', nickname='无极剑圣'}
HeroNode{no=2, name='李青', nickname='盲僧'}
HeroNode{no=3, name='艾希', nickname='寒冰射手'}
HeroNode{no=4, name='菲奥娜', nickname='无双剑姬'}
修改后的链表:
HeroNode{no=1, name='易大师', nickname='无极剑圣'}
HeroNode{no=2, name='李青', nickname='瞎子'}
HeroNode{no=3, name='艾希', nickname='寒冰射手'}
HeroNode{no=4, name='菲奥娜', nickname='无双剑姬'}

Process finished with exit code 0

Continue to modify a non-existent node: HeroNode hero5 = new HeroNode(11, "李青","瞎子");
test results:

HeroNode{no=1, name='易大师', nickname='无极剑圣'}
HeroNode{no=2, name='李青', nickname='盲僧'}
HeroNode{no=3, name='艾希', nickname='寒冰射手'}
HeroNode{no=4, name='菲奥娜', nickname='无双剑姬'}
未找到编号11的结点,不可修改
修改后的链表:
HeroNode{no=1, name='易大师', nickname='无极剑圣'}
HeroNode{no=2, name='李青', nickname='盲僧'}
HeroNode{no=3, name='艾希', nickname='寒冰射手'}
HeroNode{no=4, name='菲奥娜', nickname='无双剑姬'}

Process finished with exit code 0

Second, the deletion of the singly linked list

1. Delete the node

To delete a node, you still have to find the node first.

As shown in the figure, I want to delete node 4, traverse with the help of temp, and find the node to be deleted. However, temp cannot point to node 4, it must point to the one before node 4.
Because this is a singly linked list, node 4 records the location information of the next node, so it cannot be deleted at node 4. It should point to node 1, so that
the next pointer of node 1 can be modified to bypass node 4 and point to node 8.

At this time, node 4, because there is no other reference to it, it is recycled by the garbage collection mechanism, and the deletion of node 4 is completed.
Continue to use code to simulate, add a new node deletion method:

    // 删除结点
    public void delete(int no) {
        HeroNode temp = headNode;
        boolean flag = false;  // 标记是否找到待删除的结点的前一个结点
        while (true) {
            if (temp.next == null) {
                break;
            }
            if (temp.next.no == no) {
                // 找到待删除结点的前一个结点
                flag = true;
                break;
            }
            temp = temp.next; // temp后移操作

        }
        if (flag) {
            temp.next = temp.next.next;
        } else {
            System.out.printf("要删除的结点%d 不存在
", no);
        }
    }

Test deletion, I deleted the 1 and 4 at the beginning and the end.

删除后的链表:
HeroNode{no=2, name='李青', nickname='瞎子'}
HeroNode{no=3, name='艾希', nickname='寒冰射手'}

Process finished with exit code 0

I delete all the nodes in the linked list:

删除后的链表:
链表为空

Process finished with exit code 0

The results of the two cases are in line with expectations.

Three, the advantages and disadvantages of singly linked list structure and sequential storage structure

  1. The
    time complexity of looking up a singly linked list is O(n), while the sequential storage structure is O(1), which is more advantageous.
  2. Insertion and deletion of
    sequential structure requires moving half of the elements of the table on average, and the time complexity is 0(n).
    After the singly linked list finds the target position, the time complexity of insert and delete operations is O(1), which is more efficient.

There is no perfect solution, and all have their own advantages, so the specifics still have to be determined according to actual needs.
For example, the requirement is high-frequency search, with few additions and deletions, and the sequential structure is more suitable. On the contrary, the singly linked list structure is more appropriate.

Guess you like

Origin blog.csdn.net/weixin_48967543/article/details/115240328