Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 创建具有不同结构类型的链表_C_Linked List - Fatal编程技术网

C 创建具有不同结构类型的链表

C 创建具有不同结构类型的链表,c,linked-list,C,Linked List,我正在定义一个链表,其中结构的上半部分(第一个)的类型与其他类型不同。第一个节点始终存在 Type1 -> Type2_1 -> Type2_2 -> Type2_3->etc 我必须能够重新排列Type2元素。例如,我可以: Type1 -> Type2_3 -> Type2_1 -> Type2_2 -> etc 我尝试这样做的方式是定义一个双链接列表。每个Type2元素都可以指向下一个Type2、上一个,如果需要,还可以指向Type1。

我正在定义一个链表,其中结构的上半部分(第一个)的类型与其他类型不同。第一个节点始终存在

Type1 -> Type2_1 -> Type2_2  -> Type2_3->etc
我必须能够重新排列Type2元素。例如,我可以:

Type1 -> Type2_3 -> Type2_1 -> Type2_2 -> etc
我尝试这样做的方式是定义一个双链接列表。每个Type2元素都可以指向下一个Type2、上一个,如果需要,还可以指向Type1。如果Type2元素位于Type1旁边,则其指向前一个Type2的指针设置为NULL

 typedef struct Type2{
        struct Type2 *next;
        struct Type2 *previous;
        int isNextToType1;
    } Type2;
有没有更好的办法

typedef struct Type2
{
  ...
  struct Type2 *previous; // This is NULL for the first element
  struct Type2 *next; // This is NULL for the last element
} Type2;

typedef struct Type1
{
  ...
  struct Type2* list; // This might be NULL if the list is empty
} Type1;

似乎您不需要更多的东西。

如果上一个指针设置为
NULL
,您需要
isnextottype1
成员做什么?只需检查
NULL
previous
指针。@Drax这应该是一个答案。最好将此数据描述为包含链接列表(Type2s)的结构(Type1)。这样想可能会促使你朝着更有效的方向前进。