这里定义了一个node类型的结构体,这个结构体包含了是一个链表类型的,里面有一些参数。
node是一种类型,跟常用的int,char是一样的。而node list之后list才是链表的参数,跟定义int a里面的a一样,只是a是整型参数,list是自定义的链表node型参数!
不知道能明白了不?
typedef struct Node node; 这里的作用就是将结构体 struct Node 定义别名为 node; ,就像给人起“绰号”一样,方便后面代码的书写。使用关键字 typedef 可以为类型起一个新的别名,typedef 的用法一般为:typedef oldName newName;。上面代码里,要定义一个结构体链表变量,需 struct Node list; 这样书写,用typedef 定义别名后,直接:node list; 就简单多了。
不知道你这个问题是否已经解决, 如果还没有解决的话:在C语言中,引用node list时需要使用&list的原因是因为node list是指针类型的结构体数组,其元素是存放节点地址的指针。引用list时,&符号表示取地址符号,即获取指向list的指针变量的地址,这样才能对list指针变量进行操作,如传递给函数、修改node list的内容等。具体操作示例如下:
#include <stdio.h>
#include <stdlib.h>
struct node {
struct node *next; // 指向下一个节点的指针
int data; // 节点的数据
};
int main() {
struct node *head = NULL; // 定义链表头为空
struct node *tail = NULL; // 定义链表尾为空
// 创建一个新节点
struct node *new = (struct node *) malloc(sizeof(struct node));
new->data = 10;
new->next = NULL;
// 如果链表为空,则将头和尾都指向新节点
if (head == NULL) {
head = new;
tail = new;
}
// 如果链表不为空,则将尾节点指向新节点
else {
tail->next = new;
tail = tail->next;
}
// 遍历链表并输出节点数据
struct node *current = head;
while (current != NULL) {
printf("Node data: %d\n", current->data);
current = current->next;
}
return 0;
}