简体   繁体   English

为什么单向链表需要嵌套结构?

[英]Why nested structure is needed in singly linked list?

I'm new to data structures and algorithms.我是数据结构和算法的新手。 Right know I'm learning singly linked lists and I'm confused with a piece of code that creates a linked list.正确知道我正在学习单链表并且我对一段创建链表的代码感到困惑。

#include<stdio.h>
#include<stdlib.h>

struct node
{
    int data;
    struct node *next;
};

int main()
{
    int n;
    struct node *head = NULL;
    head = malloc(sizeof(struct node));

    head->data = 36; 
    head->next = NULL;

    printf("%d", head->data);
}

My question is why we have to use struct node *next;我的问题是为什么我们必须使用struct node *next; part in order to refer to memory location of next node.部分为了参考下一个节点的 memory 位置。
Why can't we instead use int *next ?为什么我们不能改用int *next

This definition这个定义

struct node
{
    int data;
    struct node *next;
};

explains解释
"This is a node, carrying info on an int and pointing to another node (if not NULL), which in turn might point to yet another node... and so on." “这是一个节点,在一个 int 上携带信息并指向另一个节点(如果不是 NULL),这又可能指向另一个节点......等等。”
A typical linked list.一个典型的链表。

This is what I understand your alternative to be:这就是我对您的替代方案的理解:

struct node
{
    int data;
    int *next;
};

And it would explain:它会解释:
"This is a node, carrying info on an int and pointing to another int (if not NULL)." “这是一个节点,在一个 int 上携带信息并指向另一个 int(如果不是 NULL)。”
But an int cannot provide access to yet another int.但是一个 int 不能提供对另一个 int 的访问。 So this one can provide access to info on one or two ints.所以这个可以提供对一个或两个整数的信息的访问。
The idea of a linked list, in contrast, is however that it can (assuming memory) provide access to an indeterminate and quite high number of nodes.然而,链表的想法是它可以(假设内存)提供对不确定且数量相当多的节点的访问。

In fact, you may write like事实上,你可以这样写

struct node
{
    int data;
    int *next;
};

To allocate the head node you can write (as you already did)要分配您可以编写的头节点(就像您已经做的那样)

head = malloc(sizeof(struct node));

head->data = 36; 
head->next = NULL;

If you want to allocate the next node you have to write如果你想分配你必须写的下一个节点

head->next = malloc( sizeof( struct node ) );

But to assign values to the newly created node you have to write但是要为新创建的节点赋值,您必须编写

( ( struct node * )head->next )->data = 37;
( ( struct node * )head->next )->next = NULL;

Such a code will only confuse readers of the code.这样的代码只会让代码的读者感到困惑。

That is you are allocating memory for an object of the type struct node but then you are interpreting the pointer to the allocated memory as having the type int * instead of using a pointer of the type struct node * .也就是说,您正在为类型为struct node的 object 分配 memory,但随后您将指向分配的 memory 的指针解释为具有类型int *而不是使用类型为struct node *的指针。 And then again to assign values tp an object of the type struct node you need to cast the pointer of the type int * to the type struct node * .然后再次为 object 类型的struct node赋值,您需要将int *类型的指针转换为struct node *类型。 Such a code is error-prone.这样的代码很容易出错。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM