【问题标题】:How would you link a linked list with different struct你将如何链接一个具有不同结构的链表
【发布时间】:2013-03-17 22:43:22
【问题描述】:

我有两个不同的结构

    typedef struct name {

    char*markerName;
    struct test *next;

}name_t;

 typedef struct test {

    int grade;
    int studentNumber;
    struct test *next;


}test_t;

还有这个功能

void test(name_t* marker1,int data)
{
        test_t *temp= malloc(sizeof(test_t));
        test_t *location=NULL;
        temp->grade=data;
        temp->next=NULL;
        location=marker1->next;
        if(location==NULL)
        {
        //  printf("%i \n",temp->grade);
            marker1->next=temp;
        }
        else
        {
            while(location!=NULL)
            {
                printf("%i \n",location->grade);
                printf("%p \n",location->next);
                location=location->next;
            }
            location=temp;
        }
}

问题是我们正在创建一个结构名称的数组,并在数组的每个元素之后创建一个测试的链表。如何将结构名称的节点链接到结构测试?

我打印了下一个,它们一直指向 NULL 指针。

【问题讨论】:

    标签: c linked-list


    【解决方案1】:

    严格来说,链表只能包含一种数据类型。如果您想要一个包含两种结构类型的列表,您可以使用联合来模拟:

    struct name {
       char* markerName;
    };
    
    struct test {
       int grade;
       int studentNumber;
    };
    
    // Indicates the type of data stored in the union
    enum dtype { NONE, NAME, TEST };
    
    // Combination of the above structures, suitable for a mixed-type list
    struct combo {
       struct combo*   next; // Next structure in the linked list
       enum dtype      type; // Indicates which of the union fields is valid
       union {
          struct name  name;
          struct test  test;
       };
    };
    

    这会将两组数据存储在一个结构中,允许您从这些结构中创建列表,并使您能够跟踪当前有效的数据类型。

    【讨论】:

    • 所以除了这个方法之外,没有办法用一种类型的头部和身体的其余部分开始一个链表吗?
    • 哦,谢谢,Union 帮了大忙。我讨厌教科书只给你代码(和一半)并假设我们都是 c 向导并且可以弄清楚他们在说什么。这个工会做得很好再次感谢
    • @user2135885- 如果列表顶部恰好包含一个struct name,而其余的都是struct test,则不需要基于联合的方法。你可以有一个struct name,它有一个指向struct test 对象列表的指针(name 不会是列表的一部分,它只会指向它)。但是,如果您想要每种结构类型中的一种以上,那么您将需要更复杂的东西(例如联合)。
    【解决方案2】:

    您超出了链接列表的末尾。你的location 变量最终得到'NULL',即使它可以被分配,它仍然是一个局部变量,当你的函数退出时会脱离上下文。您的 while 循环应该看起来更像这样:

    while(location->next != NULL)
    {
        printf("%i \n",location->grade);
        printf("%p \n",location->next);
        location = location->next;
    }
    
    location->next = temp;
    

    【讨论】:

      【解决方案3】:

      您可以使用指针来键入void。当然,这假设您以某种方式知道下一个对象的类型。

      当你想创建一个异构数据结构时,最好只有一种结构类型的节点,并且在节点中有两个“有效载荷”变量,一个告诉你节点的类型,一个指针到具有实际数据的结构。

      【讨论】:

      • 你会怎么做呢?只是 void ptr in name_t 并像 test_t *current = (test_t)(stuctname -> ptr) 一样调用它?还有为什么我的方法不起作用?
      【解决方案4】:

      如果结构体有两种类型的 next 指针:一种是 name_t 类型,另一种是 test_t 类型。您可以使用您想要链接的一个并将另一个保留为 NULL。希望我正确理解了您的问题。

      【讨论】:

      • 我想知道如何将它们与始终以名称开头的链表链接。并在每次标记时增加测试次数。如果没有标记测试,则为 NULL。我似乎无法从名称链接到测试
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-09
      • 2018-05-17
      相关资源
      最近更新 更多