链表在C语言中的基本结构

huangapple go评论57阅读模式
英文:

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 &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

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-&gt;data = 2;
    temp-&gt;next = NULL;
    A = temp;
    printf(&quot;%d&quot;, A);
    return 0;
}

I have understood how pointers work, for example:

    //Example 2
    int a = 2;
    int* p = &amp;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-&gt;data = 2 and A-&gt;next = NULL, I was expecting A-&gt;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 &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

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-&gt;data = 2;
    temp-&gt;next = NULL;
    A-&gt;next = temp;
    Node* temp1 = malloc(sizeof * temp1);
    temp1-&gt;data = 3;
    temp1-&gt;next = NULL;
    temp-&gt;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->nexttemp->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;

ANULL。它没有指向任何节点。因此,尝试设置 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-&gt;next? You never assign temp to A-&gt;next. You assign NULL to A-&gt;next (temp-&gt;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 aka temp-&gt;data is its data field (currently containing 2).
  • Since A has the same value as temp, (*A).data aka A-&gt;data can also be used to access its data field.
  • (*temp).next aka temp-&gt;next is the address of the next node (currently NULL).
  • Since A has the same value as temp, (*A).next aka A-&gt;next can also be used to access its next field.
Node *temp               Node anon
@ 0x1000                 @ 0x2000
+------------+           +------------+
| 0x2000  ---------+----&gt;| 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-&gt;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.

huangapple
  • 本文由 发表于 2023年2月9日 01:40:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75389722.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定