用于编写删除链表的函数的 C 程序
C的算法:
遍历链表,将所有节点一一删除。这里的要点是,如果当前指针被删除,则不要访问当前指针的next。
执行:
C
// C program to delete a linked list
#include
#include
#include
// Link list node
struct Node
{
int data;
struct Node* next;
};
// Function to delete the entire
// linked list
void deleteList(struct Node** head_ref)
{
// define head_ref to get the real head
struct Node* current = *head_ref;
struct Node* next;
while (current != NULL)
{
next = current->next;
free(current);
current = next;
}
// Define head_ref to affect the real
// head back in the caller.
*head_ref = NULL;
}
/* Given a reference (pointer to pointer) to
the head of a list and an int, push a new
node on the front of the list. */
void push(struct Node** head_ref,
int new_data)
{
// Allocate node
struct Node* new_node =
(struct Node*) malloc(sizeof(struct Node));
// Put in the data
new_node->data = new_data;
// Link the old list off the new node
new_node->next = (*head_ref);
// Move the head to point to the new node
(*head_ref) = new_node;
}
// Driver code
int main()
{
// Start with the empty list
struct Node* head = NULL;
// Use push() to construct list
// 1->12->1->4->1
push(&head, 1);
push(&head, 4);
push(&head, 1);
push(&head, 12);
push(&head, 1);
printf("Deleting linked list");
deleteList(&head);
printf("Linked list deleted");
}
输出:
Deleting linked list
Linked list deleted
时间复杂度: O(n)
辅助空间: O(1)
有关详细信息,请参阅有关编写函数以删除链接列表的完整文章!