C 中链表的结构
很抱歉问了这么愚蠢的问题,但我真的很困惑。
struct Amit
{
int a;
struct Amit *link;
}
*start;
这里*link
和*start
都是用来指向链表的一个节点,但是这两个有什么区别,为什么我们不能把*start
在结构体内?
Sorry for asking such a stupid question but I am really confused.
struct Amit
{
int a;
struct Amit *link;
}
*start;
Here both *link
and *start
are used to point to a node of a linked list, but what's the difference between these two and why can't we put *start
inside the structure body?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
link
是结构类型的成员。每个struct Amit
类型的结构都有一个。start
是“指向struct Amit
的指针”类型的变量。在任何给定时间,最多可以有一个名为start
的变量可见。您可以将
start
放入结构中,但它将成为结构的成员(如link
),并且您仍然需要声明结构类型的变量,或者指向他们的指针。这个想法是,列表中除最后一个之外的每个结构都包含一个指向列表中下一个结构的
link
指针。通常,列表中最后一个结构的link
指针为 NULL (0)。向下搜索列表时,您会查看值,当您需要下一项时,您可以按照link
找到它,当link
为 NULL 时停止。可以改为构建一个循环链表,它具有不同的停止标准。
查看注释,并解释更多一点...
创建列表的一种方法是:
变量
root
是一个用root.a == 0
初始化的结构,并且root.link == NULL
(或者等效地,root.link == 0
)。指针变量start
指向(存储)root
的地址。给定一个新节点:我们可以将其添加到
start
指向的列表的前面:创建列表的更合理的方法是动态分配节点,包括根节点。一致性至关重要,因为您必须释放动态分配的节点,并且动态分配某些节点而另一些则不动态分配会很混乱。 (我假设函数
void *emalloc(size_t nbytes);
是malloc()
的一个覆盖函数,它永远不会返回空指针 - 所以它会进行错误检查对我来说。)您通常会将这些东西打包成管理节点分配、初始化和链接的函数。
ETC。
The
link
is a member of the structure type. Every structure of typestruct Amit
has one.The
start
is a variable of type 'pointer tostruct Amit
'. At any given time, there can be at most one variable calledstart
visible.You could put
start
inside the structure, but it would become a member of the structure (likelink
), and you would still need to declare variables of the structure type, or pointers to them.The idea is that each structure on a list except the last contains a
link
pointer to the next structure on the list. Normally, the last structure on the list has alink
pointer that is NULL (0). When searching down a list, you look at the values, and when you need the next item, you follow thelink
to it, stopping when thelink
is NULL.It is possible to build a circular linked list instead, which has a different stop criterion.
Looking at the comments, and explaining a bit more...
One way to create a list is:
The variable
root
is a structure initialized withroot.a == 0
androot.link == NULL
(or, equivalently,root.link == 0
). The pointer variablestart
points to (stores the address of)root
. Given a new node:we can add that to the front of the list which
start
points to:A more plausible way to create a list is by dynamically allocating nodes, including the root node. Consistency is crucial because you have to free the dynamically allocated nodes, and having some nodes dynamically allocated and others not is messy. (I'm assuming that function
void *emalloc(size_t nbytes);
is a cover function formalloc()
that never returns a null pointer - so it does the error checking for me.)You'd normally package this stuff up into functions which manage the allocation, initialization and linking of the nodes.
Etc.
这基本上定义了三件事:
link
start
您可以通过将结构体的定义与
start
变量的声明分开来减少混乱,如下所示:This basically defines three things:
struct
(don't capitalize it as Struct, by the way)link
start
You can reduce the confusion by separating the definition of the struct from the declaration of the
start
variable, like this:如果您将“链接”重命名为“下一个”,可能会帮助您更好地理解它。链表就像一条链 - 您的“开始”(或通常称为列表“头”)是链的第一个环,链的下一个环通过您的“下一个”指针链接到它(在你的情况下,你的“链接”指针)。当没有其他环时(链接为 NULL),您知道您到达了链上的最后一个项目。
If you rename "link" to "next" it might help you get a better sense of it. A linked list is like a chain - your "start" (or as usually called, the list "head") is the first ring of the chain, and the next ring of the chain is linked to it through your "next" pointer (in your case, your "link" pointer). You know you got to the last item on your chain when there are no other rings (link is NULL).
Start 指向列表顶部,并且可供您的程序全局使用。而链接仅跟踪下一个项目,并且在引用特定“节点”时可用。看看这张图,也许可以帮助你直观地理解!
link 在内部跟踪后续项目,该项目跟踪下一个组件的位置,因为它不一定像数组那样连续。
希望这有助于澄清。
Start points to the top of the list and is available globally to your program. Whereas link just keeps track of the next item, and is available when referring to a specific 'node'. See this diagram it may help you understand with a visual!
link internally tracks the following item which keeps track of where the next component is as it is not necessarily contiguous the way arrays are.
Hope that helps clarify.