0

I am defining a linked list where on of the structure (the first one) has a different type from the others. This first node always exists.

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

I must be able to rearrange the Type2 elements. For example, I can have:

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

The way I am trying to do this is by defining a double linked list. Each Type2 element can point to the next Type2, the previous and, if need be the Type1. If the Type2 element is next to the Type1, then its pointer to the previous Type2 is set to NULL.

 typedef struct Type2{
        struct Type2 *next;
        struct Type2 *previous;
        int isNextToType1;
    } Type2;

Is there any better way to do this?

3
  • 2
    if the previous pointer is set to NULL, what do you need the isNextToType1 member for ? just check for a NULL previous pointer. Commented Oct 8, 2014 at 15:37
  • @Drax that should be an answer. Commented Oct 8, 2014 at 16:38
  • You would be better off characterizing this data as a structure (Type1) containing a linked list (of Type2s). Thinking of it that way is likely to prompt guide you in more effective directions. Commented Oct 8, 2014 at 16:41

1 Answer 1

2
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;

Seems like you don't need anything more than this.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.