`target->next`指的是`target`结构体内的`next`域。
下面是一个示例代码,定义了一个包含`data`和`next`两个域的结构体`Node`:
- #include <stdio.h>
- #include <stdlib.h>
- struct Node {
- int data;
- struct Node* next;
- };
- int main() {
- // 创建三个节点
- struct Node* first = (struct Node*)malloc(sizeof(struct Node));
- struct Node* second = (struct Node*)malloc(sizeof(struct Node));
- struct Node* third = (struct Node*)malloc(sizeof(struct Node));
- // 为每个节点赋值
- first->data = 1;
- second->data = 2;
- third->data = 3;
- // 设置节点之间的链接关系
- first->next = second;
- second->next = third;
- third->next = NULL; // 最后一个节点的next指针为NULL
- // 遍历链表并打印节点的值
- struct Node* current = first;
- while (current != NULL) {
- printf("%d ", current->data);
- current = current->next;
- }
- // 释放内存
- free(first);
- free(second);
- free(third);
- return 0;
- }
复制代码
这个例子中创建了一个简单的链表,其中每个节点都包含一个整数值和一个指向下一个节点的指针。通过遍历链表,可以按顺序打印出每个节点的值。最后记得释放内存以防止内存泄漏。
以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。