英文:
linked list basic architecture in C
问题
以下是您要翻译的内容:
I am new to C and I want to implement the linked list. This is my initial copied code:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data; // integer data
struct Node* next; // pointer to the next node
} Node;
int main() {
Node* A = NULL;
Node* temp = malloc(sizeof *temp);
temp->data = 2;
temp->next = NULL;
A = temp;
printf("%d", A);
return 0;
}
I have understood how pointers work, for example:
//Example 2
int a = 2;
int* p = &a;
such that p
holds the address of a
and *p
holds the content of it.
In the node example, the basic idea is to create an initial node, and then from there, to link other nodes when inserting at the end for instance. So, when we did this:
Node* A = NULL;
Node* temp = malloc(sizeof *temp);
we create a node A
, my first question here, why I can't use the same concept of accessing its address and content NULL
the same as in Example 2
or how can I do that?
Second, when we created node temp
and assigned 2
to its data, and NULL
to its next, it's all clear, but when we did A = temp
, this is not clear, what did we assigned exactly? I mean, how can I get from A
to the next node, A
now has A->data = 2
and A->next = NULL
, I was expecting A->next
to store the address of temp
, no? Please if you can explain in the simplest terms the basic abstract inner workings?
Update
For example, if I want to create a linked list with two elements 2
and 3
. Is the following code correct?
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data; // integer data
struct Node* next; // pointer to the next node
} Node;
int main() {
Node* A = NULL;
Node* temp = malloc(sizeof *temp);
temp->data = 2;
temp->next = NULL;
A->next = temp;
Node* temp1 = malloc(sizeof *temp1);
temp1->data = 3;
temp1->next = NULL;
temp->next = temp1;
return 0;
}
英文:
I am new to C and I want to implement the linked list. This is my initial copied code:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data; // integer data
struct Node* next; // pointer to the next node
} Node;
int main() {
Node* A = NULL;
Node* temp = malloc(sizeof * temp);
temp->data = 2;
temp->next = NULL;
A = temp;
printf("%d", A);
return 0;
}
I have understood how pointers work, for example:
//Example 2
int a = 2;
int* p = &a;
such that p
holds the address of a
and *p
holds the content of it.
In the node example, the basic idea is to create an initial node, and then from there, to link other nodes when inserting at the end for instance. So, when we did this:
Node* A = NULL;
Node* temp = malloc(sizeof * temp);
we create a node A
, my first question here, why I can't use the same concept of accessing its address and content NULL
the same as in Example 2
or how can I do that?
Second, when we created node temp
and assigned 2
to its data, and NULL
to its next, it's all clear, but when we did A = temp
, this is not clear, what did we assigned exactly? I mean, how can I get from A
to the next node, A
now has A->data = 2
and A->next = NULL
, I was expecting A->next
to store the address of temp
, no? Please if you can explain in the simplest terms the basic abstract inner workings?
Update
For example, if I want to create a linked list with two elements 2
and 3
. Is the following code correct?
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data; // integer data
struct Node* next; // pointer to the next node
} Node;
int main() {
Node* A = NULL;
Node* temp = malloc(sizeof * temp);
temp->data = 2;
temp->next = NULL;
A->next = temp;
Node* temp1 = malloc(sizeof * temp1);
temp1->data = 3;
temp1->next = NULL;
temp->next = temp1;
return 0;
}
答案1
得分: 1
-
temp
包含了由malloc
分配的节点的地址。 -
你将这个地址分配给了
A
。(A = temp;
) -
所以
A
包含了由malloc
分配的节点的地址。 -
什么是下一个节点?你只创建了一个节点。
-
你期望
A->next
存储temp
的地址吗?在任何时候都没有获取到temp
的地址。也没有这个必要。所以我认为你指的是节点的地址(在temp
中的地址)。但为什么会在A->next
中呢?你从未将temp
分配给A->next
,而是将NULL
分配给了A->next
(temp->next = NULL;
)。 -
如果你能以最简单的方式解释基本的内部工作原理,那将会怎么样呢?
temp
包含了你分配的节点的地址。- 你将这个地址分配给了
A
,所以它包含了相同的地址。 *temp
和*A
是你分配的节点。(*temp).data
也就是temp->data
是它的data
字段(当前包含2
)。- 由于
A
具有与temp
相同的值,(*A).data
也就是A->data
也可以用来访问其data
字段。 (*temp).next
也就是temp->next
是下一个节点的地址(当前为NULL
)。- 由于
A
具有与temp
相同的值,(*A).next
也就是A->next
也可以用来访问其next
字段。
Node *temp Node 匿名
@ 0x1000 @ 0x2000
+------------+ +------------+
| 0x2000 ---------+---->| data: 2 |
+------------+ | | next: NULL |
| +------------+
Node *A |
@ 0x1004 |
+------------+ |
| 0x2000 ---------+
+------------+
(地址完全是虚构的,当然了。)
- 以下代码是正确的吗?
不正确。你有:
Node* A = NULL;
...
A->next = temp;
A
是 NULL
。它没有指向任何节点。因此,尝试设置 A
指向的节点的 next
字段没有意义。这是未定义的行为。
英文:
> when we did A = temp, this is not clear, what did we assigned exactly?
temp
contains the the address of the node allocated by malloc
.
You assigned this to A
. (A = temp;
)
So A
contains the the address of the node allocated by malloc
.
> Now can I get from A to the next node
What next node? You only ever created one node.
> I was expecting A->next to store the address of temp, no?
At no point do you get the address of temp
. And there's no point in doing that. So I think you mean the address of the node (the address in temp
). But why wou ld that be in A->next
? You never assign temp
to A->next
. You assign NULL
to A->next
(temp->next = NULL;
).
> Please if you can explain in the simplest terms the basic abstract inner workings?
temp
holds the address of the node you allocated.- You assign this to
A
, so it holds the same address. *temp
and*A
is the node you allocated.(*temp).data
akatemp->data
is itsdata
field (currently containing2
).- Since
A
has the same value astemp
,(*A).data
akaA->data
can also be used to access itsdata
field. (*temp).next
akatemp->next
is the address of the next node (currentlyNULL
).- Since
A
has the same value astemp
,(*A).next
akaA->next
can also be used to access itsnext
field.
Node *temp Node anon
@ 0x1000 @ 0x2000
+------------+ +------------+
| 0x2000 ---------+---->| data: 2 |
+------------+ | | next: NULL |
| +------------+
Node *A |
@ 0x1004 |
+------------+ |
| 0x2000 ---------+
+------------+
(The addresses are completely made up, of course.)
> Is the following code correct?
No. You have:
Node* A = NULL;
...
A->next = temp;
A
is NULL
. It doesn't point to a node. So attempting to set the next
field of the node to which A
points makes no sense. This is undefined behaviour.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论