【问题标题】:linked list syntax in C [closed]C中的链表语法[关闭]
【发布时间】:2016-03-13 23:58:53
【问题描述】:

我试图通过图表来理解链表的语法。但是我越来越困惑。此代码打印出 0-9。注释的部分看不懂,请直观说明。

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

struct node
{
    int item;
    struct node* link;
};

int main()
{
    struct node *start,*list;
    int i;
    start = (struct node *)malloc(sizeof(struct node)); //???????
    list = start; // ????????
    start->link = NULL; // ??????????
    for(i=0;i<10;i++)
    {
        list->item = i;
        list->link = (struct node *)malloc(sizeof(struct node));
        list = list->link;
    }
    list->link = NULL; //????????????
    while(start != NULL) //??????????????
    {
        printf("%d\n",start->item);
        start = start->link;
    }
    return 0;
} 

【问题讨论】:

  • 请阅读算法和数据结构书籍或只是搜索网络。有很多资源可以解释链接列表,包括视觉。
  • 我了解链表,并且阅读了很多关于链表的资源,但是每个链接似乎使用不同的语法来解释,所以剩下的就是我开始时感到更加困惑。

标签: c linked-list


【解决方案1】:
int main()
{
    struct node *start,*list;
    int i;
    // Allocate memory for a single node
    start = (struct node *)malloc(sizeof(struct node)); //???????
    // The head of the list is starting node.
    // (This head represented by `list` will be advanced when building;
    //  while the head represented by `start` will not be advanced
    //  until iterating for display.)
    list = start; // ????????
    // The starting node has no 'next' yet.
    // (This is not technically needed in this exact code.
    //  The code would have been more clear with the assignment of
    //  null to the 'next' after each malloc to start with a clean node.)
    start->link = NULL; // ??????????
    for(i = 0; i < 10; i++)
    {
        list->item = i;
        list->link = (struct node *)malloc(sizeof(struct node));
        list = list->link; // Move to the next (newly created) node.
    }
    // The last node has no 'next' node. Need to manually assign it
    // a value because it "could be anything" until properly initialized.
    // (Intermediate nodes have had the 'next' assigned in the loop.)
    list->link = NULL; //????????????

    // Iterate over the list, using `start` to track the head and
    // keep iterating until the end of the list is reached.
    while(start != NULL) //??????????????
    {
        printf("%d\n",start->item);
        start = start->link; // Move to the 'next' node
    }
    return 0;
}

将其与以下内容进行比较,希望能够使目标/意图更加清晰:

struct node *create_node(int value) {
    struct node *n = malloc(sizeof(struct node));
    n->item = value; // all nodes start with some value
    n->link = NULL;  // all nodes start without a 'next'
    return n;
}

int main()
{
    struct node *start,*list;
    int i;

    // Create list of elements 0..9
    // (Creating the start node first avoids a condition check;
    //  pulling out the value assignment from the subsequent/trailing loop
    //  is for clarity.)
    start = create_node(0);
    list = start;
    for(i = 1; i < 10; i++) // Changed loop count - value assigned in create
    {
        list->link = create_node(i);
        list = list->link;
    }

    // Display - note reset of tracked head to beginning of list
    list = start;
    while(list != NULL)
    {
        printf("%d\n", list->item);
        list = list->link;
    }
    return 0;
}

【讨论】:

  • 非常感谢您提供的新代码,它绝对清晰。但是,我无法理解参考资料。例如 Struct node *start 是一个指向结构节点的变量 start。结构节点 *list 也是如此。但是 list = start 是什么意思呢?这是否意味着您将 start 它作为一个指向结构的指针并将其等同于 list ,它是一个指向它自己的结构的指针,但现在指向 start 而不是?
  • @Hyune x = y 始终是一项任务。在这种情况下,它分配 pointer 的值,意思是,在list = start 之后,两个 pointer 都指向同一个对象/结构(刚刚在第一个分配的那个案子)。指针变量(例如struct node *)必须在使用前分配有意义的值。真正在内存中分配节点对象的是malloc(这些对象是通过返回的指针访问的)。
  • 非常感谢!我现在明白了^^
猜你喜欢
  • 2020-04-11
  • 1970-01-01
  • 1970-01-01
  • 2012-02-05
  • 1970-01-01
  • 1970-01-01
  • 2016-04-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多