我正在定义一个链表,其中结构的on(第一个)具有与其他类型不同的类型。第一个节点总是存在的。

Type1 -> Type2_1 -> Type2_2  -> Type2_3->etc

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

我尝试的方法是定义一个双链接列表。每个Type2元素都可以指向下一个Type2,前一个,如果需要的话是Type1。如果Type2元素位于Type1旁边,则指向前一个Type2的指针设置为空。
 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;

看来你不需要什么了。

关于c - 创建具有不同结构类型的链表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26260801/

10-11 19:33