在C语言中,使用 **head
作为函数参数意味着您希望该函数能够修改指针本身的值,即改变指针指向的地址。这通常称为 "间接引用" 或 "双指针"(double pointer)。
解释 appendNode
函数中的 **head
:
-
指针的指针:
ListNode** head
表示head
是一个指向ListNode*
类型的指针的指针。换句话说,它是一个指向指针的指针。 -
修改头指针: 在链表操作中,有时需要在函数内部修改链表的头指针(例如,将新节点添加到链表的头部)。由于C语言的参数传递是通过值传递的,直接传递
head
会导致head
的副本被创建,函数内部对head
的修改不会影响到原始的head
指针。 -
使用双指针: 通过传递
head
的地址(即&head
),您可以在函数内部修改head
指针本身的值。在appendNode
函数中,**head
允许我们修改原始head
指针的值,即改变它指向的链表的头部。 -
函数调用示例: 当您调用
appendNode(&head, data)
时,您实际上是传递了head
指针的地址。函数内部通过*head
来访问head
指针指向的值(即链表的当前头节点),并可能修改它来指向新添加的节点。 -
为什么这样做: 使用双指针允许函数在必要时重新分配链表头节点的内存,同时确保调用者在函数调用后能够看到这些更改。
ListNode`:这是一个自定义的结构体类型,代表链表中的一个节点。它通常包含数据字段和指向同类型结构体的指针字段(可能还有前一个节点的指针,如果是双向链表的话)。
*:在类型名称后面的星号表示这是一个指向 ListNode
的指针。在C语言中,星号用来表示指针类型。
所以ListNode其实是指向数据域的,然后ListNode*指向指针域
想修改指针域的值让所以需要定义一个ListNode**,在既不改变原有状态请款下创造新的尝试,如果没改动一切没问题,如果有改动也不影响之前的值
链表函数通常是指一组用于操作链表的函数,包括但不限于创建节点、添加节点、删除节点、搜索节点、打印链表等。以下是一些基本的链表操作函数的示例,使用C语言实现:
1. 定义链表节点结构体
typedef struct ListNode {
int data; // 节点存储的数据
struct ListNode* next; // 指向链表中下一个节点的指针
} ListNode;
2. 创建新节点的函数
ListNode* createNode(int data) {
ListNode* newNode = (ListNode*)malloc(sizeof(ListNode));
if (newNode != NULL) {
newNode->data = data;
newNode->next = NULL;
}
return newNode;
}
3. 向链表末尾添加新节点的函数
void appendNode(ListNode** head, int data) {
ListNode* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
} else {
ListNode* current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
}
4. 打印链表的函数
void printList(ListNode* head) {
ListNode* current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
5. 查找节点的函数
ListNode* findNode(ListNode* head, int target) {
ListNode* current = head;
while (current != NULL) {
if (current->data == target) {
return current;
}
current = current->next;
}
return NULL; // 如果未找到返回NULL
}
6. 删除节点的函数
void deleteNode(ListNode** head, int target) {
if (*head == NULL) return;
ListNode* current = *head;
ListNode* previous = NULL;
// 如果要删除的是头节点
if (current->data == target) {
*head = current->next; // 更新头指针
free(current);
return;
}
// 搜索要删除的节点
while (current != NULL && current->data != target) {
previous = current;
current = current->next;
}
// 如果找到了节点
if (current != NULL) {
previous->next = current->next; // 跳过当前节点
free(current);
}
}
7. 释放链表内存的函数
void freeList(ListNode* head) {
ListNode* current = head;
while (current != NULL) {
ListNode* toFree = current;
current = current->next;
free(toFree);
}
}
这些函数提供了链表的基本操作,可以根据需要进行扩展和修改。例如,可以添加插入节点到特定位置的函数,或者实现排序、反转链表等更复杂的操作。在实际使用中,确保对链表进行适当的内存管理,避免内存泄漏。
演示代码
#include <stdio.h>
#include <stdlib.h>
// 定义链表节点结构体
typedef struct ListNode {
int data; // 存储的数据
struct ListNode* next; // 指向下一个节点的指针
} ListNode;
// 创建新节点的函数
ListNode* createNode(int data) {
ListNode* newNode = (ListNode*)malloc(sizeof(ListNode));
if (newNode != NULL) {
newNode->data = data;
newNode->next = NULL;
}
return newNode;
}
// 向链表末尾添加新节点的函数
void appendNode(ListNode** head, int data) {
ListNode* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
} else {
ListNode* current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
}
// 打印链表的函数
void printList(ListNode* head) {
ListNode* current = head;
while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
}
// 查找节点的函数(返回节点的指针,如果未找到则返回NULL)
ListNode* findNode(ListNode* head, int target) {
ListNode* current = head;
while (current != NULL) {
if (current->data == target) {
return current;
}
current = current->next;
}
return NULL;
}
// 释放链表内存的函数
void freeList(ListNode* head) {
ListNode* current = head;
while (current != NULL) {
ListNode* toFree = current;
current = current->next;
free(toFree);
}
}
// 主函数
int main() {
ListNode* head = NULL; // 初始化链表头指针
// 添加一些节点到链表
appendNode(&head, 1);
appendNode(&head, 2);
appendNode(&head, 3);
// 打印链表
printList(head);
// 查找节点
ListNode* found = findNode(head, 2);
if (found != NULL) {
printf("Node with data %d found.\n", found->data);
} else {
printf("Node with data 2 not found.\n");
}
// 释放链表内存
freeList(head);
return 0;
}
#include <stdlib.h>
-
malloc(size_t size): 分配指定大小的内存块,并返回一个指向这块内存的指针。 calloc(size_t nmemb, size_t size): 分配一个数组的内存,并初始化为0。 realloc(void *ptr, size_t new_size): 改变之前分配的内存块的大小。 free(void *ptr): 释放之前分配的内存块。 exit(int status): 立即终止程序执行,并返回一个状态码。 system(const char *command): 执行一个shell命令。 atoi(const char *str): 将一个字符串转换为一个整数。 atol(const char *str): 将一个字符串转换为一个长整数。 strtol(const char *str, char **endptr, int base): 将字符串转换为长整数,支持指定基数。 rand(): 生成一个随机数。 srand(unsigned int seed): 设置随机数生成器的种子。 bsearch(const void *key, const void *base, size_t num, size_t size, int (*compar)(const void *, const void *)): 在已排序的数组中二分查找。
#include <string.h>
这些函数操作包括但不限于复制、连接、比较、搜索和修改字符串
-
strcpy(char \*dest, const char \*src):将 src 字符串复制到 dest 所指向的内存空间。 strncpy(char \*dest, const char \*src, size_t n):将 src 字符串的前 n 个字符复制到 dest。 strcat(char \*dest, const char \*src):将 src 字符串连接到 dest 字符串的末尾。 strncat(char \*dest, const char \*src, size_t n):将 src 字符串的前 n 个字符连接到 dest 的末尾。 strcmp(const char \*str1, const char \*str2):比较两个字符串 str1 和 str2。 strncmp(const char \*str1, const char \*str2, size_t n):比较两个字符串的前 n 个字符。 strlen(const char \*str):返回字符串 str 的长度,不包括结尾的空字符 \0。 strchr(const char \*str, int c):在字符串 str 中查找字符 c 的第一次出现。 strstr(const char \*haystack, const char \*needle):在字符串 haystack 中查找子字符串 needle。 strtok(char \*str, const char \*delim):使用 delim 指定的分隔符来分割字符串 str。 memset(void \*s, int c, size_t n):将 n 个字节的内存区域 s 设置为值 c。 memcpy(void \*dest, const void \*src, size_t n):将 n 个字节从 src 复制到 dest。 memmove(void \*dest, const void \*src, size_t n):将 n 个字节从 src 复制到 dest,允许重叠。