创建链接列表而不将节点声明为指针

时间:2021-01-21 07:17:52

I've been searching around for a good while now on google and a few text books and I can't seem to understand why it is, when building a linked list, that the nodes need to be pointers.

我一直在谷歌和一些教科书上寻找一段时间,我似乎无法理解为什么在构建链表时,节点需要成为指针。

Eg. If i have a node defined as:

例如。如果我有一个节点定义为:

typedef struct Node{
    int value;
    struct Node *next;
} Node;

why is it that in order to create a linked list, I would say:

为什么要创建一个链表,我会说:

Node *a = malloc(sizeof(Node));
Node *b = malloc(sizeof(Node));
a->value = 1;
b->value = 2;

a->next = b;
b->next = NULL;

rather than:

而不是:

Node a, b;
a.value = 1;
b.value = 2;

a.next = &b;
b.next = NULL;

To my understanding, the list will still be able to be referenced and traversed as normal, the only difference is the use of the dot, ampersand syntax rather than the arrow?

根据我的理解,列表仍然可以正常引用和遍历,唯一的区别是使用点,符号语法而不是箭头?

2 个解决方案

#1


2  

You can create the list in a way that you mentioned.

您可以按照提到的方式创建列表。

But you must care for the life time of the list members. If your

但是你必须关心列表成员的生命周期。如果你的

Node a, b;

节点a,b;

are in scope of a function then these are lost after the return of that function.

在函数范围内,然后在返回该函数后丢失这些函数。

When you use pointers then you usually use the heap and the instances live until they are deleted.

当您使用指针时,您通常会使用堆和实例,直到它们被删除。

#2


0  

Your first example will not work. You are declaring two Node pointers. However, since you do not initialize these to anything, it is illegal to deference them, since they don't point to anything. You must first use something like malloc to declare memory for them to point to, or assign them to a previously declared local variable. However, you must also remember to call free when you are done with the memory.

你的第一个例子不起作用。您正在声明两个Node指针。但是,由于您没有将这些内容初始化为任何内容,因此它们是非法的,因为它们不指向任何内容。您必须首先使用类似malloc的内容为它们指定内存,或者将它们分配给先前声明的局部变量。但是,您必须记得在完成内存后*呼叫。

In the second example, you are declaring two Node variables, which you use to store instances of the Node. These will be allocated on the stack if they are local variables, and they will live for as long as they are in scope. They hold valid memory, and thus you can use them in the way you have demonstrated.

在第二个示例中,您将声明两个Node变量,用于存储Node的实例。如果它们是局部变量,它们将在堆栈上分配,并且只要它们在范围内,它们就会存在。它们保存有效的内存,因此您可以按照演示的方式使用它们。

#1


2  

You can create the list in a way that you mentioned.

您可以按照提到的方式创建列表。

But you must care for the life time of the list members. If your

但是你必须关心列表成员的生命周期。如果你的

Node a, b;

节点a,b;

are in scope of a function then these are lost after the return of that function.

在函数范围内,然后在返回该函数后丢失这些函数。

When you use pointers then you usually use the heap and the instances live until they are deleted.

当您使用指针时,您通常会使用堆和实例,直到它们被删除。

#2


0  

Your first example will not work. You are declaring two Node pointers. However, since you do not initialize these to anything, it is illegal to deference them, since they don't point to anything. You must first use something like malloc to declare memory for them to point to, or assign them to a previously declared local variable. However, you must also remember to call free when you are done with the memory.

你的第一个例子不起作用。您正在声明两个Node指针。但是,由于您没有将这些内容初始化为任何内容,因此它们是非法的,因为它们不指向任何内容。您必须首先使用类似malloc的内容为它们指定内存,或者将它们分配给先前声明的局部变量。但是,您必须记得在完成内存后*呼叫。

In the second example, you are declaring two Node variables, which you use to store instances of the Node. These will be allocated on the stack if they are local variables, and they will live for as long as they are in scope. They hold valid memory, and thus you can use them in the way you have demonstrated.

在第二个示例中,您将声明两个Node变量,用于存储Node的实例。如果它们是局部变量,它们将在堆栈上分配,并且只要它们在范围内,它们就会存在。它们保存有效的内存,因此您可以按照演示的方式使用它们。