אית*_*לוי 0 c struct pointers linked-list
我对课堂上定义的链表的定义有一些疑问。
这是使用的:
typedef struct node_t {
int x;
struct node_t *next;
} *Node;
Run Code Online (Sandbox Code Playgroud)
现在,我知道以这种方式我们创建了一种使用指向结点node_t的指针的较短方法。Node将用作struct node_t*。
现在,假设我们要创建一个链接列表。例如:
Node node1 = malloc(sizeof(*node1));
Node node2 = malloc(sizeof(*node2));
Node node3 = malloc(sizeof(*node3));
node1->x = 1;
node1->next = node2;
node2->x = 4;
node2->next = node3;
node3->x = 9;
node3->next = NULL;
Run Code Online (Sandbox Code Playgroud)
这大概就是我的想象(圆圈代表结构):
现在我知道这是错误的,但我不明白为什么。我们有一个指针,node1它指向我们的结构。然后,我们指向node2,它指向另一个结构,依此类推。
另一件事是,我不明白如何在图片中使用更长的箭头。我们不应该只能从圆圈的每个下部指向结构,而不能指向结构的指针吗?这怎么可能?
如果这里有人可以使事情变得更清楚一点,将不胜感激。非常感谢。
You have three linked nodes, and additional local pointers pointing to them.
The nodes don't know anything about those local pointers though, even if it is often convenient to use their names to refer to the nodes.
Instead, they know the next node in the sequence, respectively the last node knows none.
Put another way, your image is flat-out wrong.
+---+------+
node1 --> | 1 | next |
+---+-|----+
|
v
+---+------+
node2 --> | 4 | next |
+---+-|----+
|
v
+---+------+
node3 --> | 9 | NULL |
+---+------+
Run Code Online (Sandbox Code Playgroud)