温馨提示×

c语言中current指针是什么

小樊
82
2024-09-08 23:04:12
栏目: 编程语言

在C语言中,"current"指针通常用于表示当前正在处理的数据结构(如链表、树等)中的元素

例如,在遍历链表时,我们可以使用一个名为"current"的指针来表示当前正在访问的节点。以下是一个简单的遍历链表并打印每个节点值的示例:

#include<stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node* next;
};

int main() {
    // 创建一个链表并初始化
    struct Node* head = (struct Node*) malloc(sizeof(struct Node));
    head->data = 1;
    head->next = (struct Node*) malloc(sizeof(struct Node));
    head->next->data = 2;
    head->next->next = NULL;

    // 使用current指针遍历链表
    struct Node* current = head;
    while (current != NULL) {
        printf("%d\n", current->data);
        current = current->next;
    }

    return 0;
}

在这个例子中,"current"指针用于遍历链表,并在每次迭代时指向当前正在访问的节点。

0