【问题标题】:link list in c, explanation of code lines? [closed]c中的链接列表,代码行的解释? [关闭]
【发布时间】:2013-11-12 18:29:18
【问题描述】:

我刚刚开始学习 c 中的链表。我仍然混淆了代码中的第 1 行。 1.什么是temp->data,是指针吗?多变的? 2.什么是temp->next=head,这里head的值为NULL????如果是这样,temp->next 现在变为 NULL ??? 真的搞砸了,请帮帮我。谢谢

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

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

struct test_struct* head=NULL;

int main()
{

    head = NULL;
    struct test_struct* temp = (struct test_struct*)malloc(sizeof(struct test_struct));
    if(NULL==temp)
    {
        printf("error in memory");
        return 0;
    }
    temp->data=5;    // line  1      <----------  what's going on
    temp->next=head; // line 2       <----------  what's going on here?
    head=temp;
    printf("%p\n",head);
    return 0;
}

【问题讨论】:

    标签: c list pointers linked-list


    【解决方案1】:

    什么是temp->data,它是指针吗?变量?

    好吧,让我们分解一下:

    1. 什么是温度?它被声明为

      struct test_struct* temp = ...
      

      所以我们知道它是一个指向struct test_struct 的指针。

    2. temp-&gt;data 是什么?这意味着跟随(取消引用)指针,并获取名为数据的成员。您的 test_struct 声明为

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

      所以,我们知道它有一个称为数据的整数成员。 temp-&gt;data 是对该整数的引用。


    什么是temp->next=head,这里head的值为NULL????如果是这样,temp->next 现在变为 NULL ???

    此代码 NULL 分配给指针temp-&gt;next

    如果您对这些东西感到困惑,学习在调试器中逐步完成它可能会有所帮助(就像一本好书一样)。

    【讨论】:

      【解决方案2】:

      -&gt; 运算符跟随指向结构的指针以引用结构的元素之一。在这种情况下:

      temp->data = 5;
      temp->next = head;
      

      temp 是指向struct test_struct 类型结构的指针,该结构具有名为datanext 的成员。这两个语句在temp 指向的结构中为这两个成员赋值。

      由于代码前面的head被设置为NULL,因此第二条语句确实将next成员设置为NULL

      从技术上讲,temp-&gt;datatemp-&gt;next 都是 lvalue(读作“ell value”),这意味着它们可以同时用于它们引用的值以及存储值的位置。 “l”代表“left”作为助记符,表明这些东西是你在赋值语句左侧的东西。

      【讨论】:

      • 在 temp-> next= head 中,next 指针得到 NULL 值,因为 head 为 NULL ?
      • 是的。赋值的右侧表示要存储的值。在这种情况下,值来自head 变量,我们可以看到它被赋值为NULL
      【解决方案3】:

      您的代码所做的是创建一个变量名“temp”,它是一种 test_struct*。然后它分配内存并将变量 temp 指向该内存块。这个临时变量是一个指针,指向您使用 malloc 创建的内存块。在“temp”里面它有两个变量名数据和下一个。在 C 中,访问您使用的成员 -> 运算符。 (第 1 行)您将整数 5 保存到 temp 中的数据变量中。在第 2 行中,您将 NULL 分配给 next(此时您的 head 为 null [明白了!您的 head 为 null :)])。然后你将头部指向 temp 指向的内存片段。现在如果你 printf("%d", head->data) 它将打印 5。

      我注释了您代码中的每一行。希望这会有所帮助。

      #include <stdio.h>
      #include <stdlib.h>
      struct test_struct{
          int data;
          struct test_struct *next;
      };
      struct test_struct* head = NULL; //Creates a global variable head. its type is test_struct* and it is currently set to NULL
      int main(){
          head = NULL; //
          struct test_struct* temp = (struct test_struct*)malloc(sizeof(struct test_struct));// creates a variable named temp which is a test_struct* type.
          if(NULL==temp){ //at this point if temp is NULL, it imply that above line failed to allocate memory so there is no point executing this program so we return.
              printf("error in memory");
              return 0;
          }
          temp->data=5; // in the structure test_struct there is a member variable data inside it and since the temp variable is of type test_struct, temp also has data member. so we can access it by using -> operator. since data is a integer type we can assign a number to it.
          temp->next=head; //At this point head is NULL. So we are pretty much assigning NULL to temp->next
          head=temp; //When using link list we usually keep the head pointing to the beginning of the link list.(unless its a circular link list). This line does the exact same. It points the dead to memory piece that temp pointing to. 
          printf("%p\n",head);
          return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-04-20
        • 1970-01-01
        • 1970-01-01
        • 2016-10-05
        • 1970-01-01
        • 2015-01-06
        • 2014-02-05
        • 1970-01-01
        相关资源
        最近更新 更多