首页 > 解决方案 > 如果我使用 memmove() (在 C 中)删除链表中的节点,它会更有效吗?

问题描述

我在互联网上遇到的每个关于删除链表中节点的示例,他们都使用这种方法删除第一个节点:

Algorithm to delete first node of Singly Linked List
%%Input:  head of the linked list
Begin:
    If (head != NULL) then
        toDelete ← head
        head ← head.next
        unalloc (toDelete)
    End if
End

在这段代码中,他们从那里的列表中删除了第一个节点后释放了内存区域。但我使用 memove() 删除链表的第一个节点。

...
if (key == 1){ 
    memmove(head, head->next, sizeof(node));
    return;
}
...

那么,当我使用 memmove() 时,它会自动释放内存区域吗?我的代码效率更高吗?

标签: csingly-linked-listmemory-efficientmemmove

解决方案


在您的代码中是一个指针,它是一个标量,因此使用memmove不会比简单的赋值更快。memmove函数在复制较大的对象时很有用。


推荐阅读