2

I have a linked list with another linked list in it and I want to integrate data in them but I couldn't.

Here is my code:

Structures declaration:

typedef struct BigStructure {
    UINT x;
    UINT y;

    struct SmallStructure* smallStructure;

    struct BigStructure* next;
} BigStructure;

typedef struct SmallStructure {
    UINT x;
    UINT y;

    struct SmallStructure* next;
} SmallStructure;

Structures manipulation functions:

BigStructure* addLinkedListElement(BigStructure* linkedList)
{
    if(linkedList-> next == NULL)
    {
        return NULL;
    }

    BigStructure* newLinkedList = malloc(sizeof(linkedList));
    newLinkedList->next = linkedList;
    return newLinkedList;
}

BigStructure* removeLinkedListElement(BigStructure* linkedList)
{
    //If the list is empty, we return NULL
    if(linkedList == NULL)
        return NULL;

    //If the list contains one element
    if(linkedList->next == NULL)
    {
        free(linkedList);
        return NULL;
    }

    //if the list contains at least 2 elements
    BigStructure* tmp = linkedList;
    BigStructure* ptmp = linkedList;

    /* Tant qu'on n'est pas au dernier élément */
    while(tmp->next != NULL)
    {
        //ptmp stores the address of tmp
        ptmp = tmp;
        //We move tmp (but pmpt keeps the old value of tmp)
        tmp = tmp->next;
    }

    ptmp->next = NULL;
    free(tmp);
    return linkedList;
}

BigStructure* getLinkedListElement(BigStructure* linkedList, int id)
{
    int i = 0;

    for(i=0; i<id && linkedList != NULL; i++)
    {
        linkedList = linkedList->next;
    }

    if(linkedList == NULL)
    {
        return NULL;
    }
    else
    {
        return linkedList;
    }
}

I tried the above code to access a SmallStructure variable, but I get a big number (looks like an address):

BigStructure* bigStructure = NULL;

void addBigStructure(UINT x, UINT y) {

        if(bigStructureNb == 1)
        {
            bigStructure->x = x;
            bigStructure->y = y;
        }
        else
        {
            BigStructure* newBigStructure;
            newBigStructure = (BigStructure*)addLinkedListElement((BigStructure*)&bigStructure);
            newBigStructure->x = x;
            newBigStructure->y = y;
        }
}

void addSmallStucture(UINT x, UINT y) {

    if(smallStructuresNb == 1)
    {
        bigStructure->startTrigger = malloc(sizeof(BigStructure*));
        bigStructure->startTrigger->x = x;
        bigStructure->startTrigger->y = y;
    }
    else
    {
        BigStructure* tmpBigStructure = NULL;
        tmpBigStructure = (BigStructure*)getLinkedListElement(&bigStructure, rowID); //Table row ID
        g_print("%d", tmpBigStructure->id); //Here I get a false value !!!!
        //Here I want to set the value of the tmpBigStructure->smallStructure->x/y
}
}
5
  • How come when you're adding a linked list element, you return if the next element is NULL? Shouldn't you be walking down the list until the next value is NULL, and then put a new linked list element where the next pointer points to? Commented May 31, 2013 at 20:16
  • I corrected it, thanks ! I still have a problem now with the "child" linked-list. Commented May 31, 2013 at 21:00
  • 1
    Ill add a response tomorrow; well figure this out. Commented May 31, 2013 at 22:11
  • Just a debug comment: Did you try & dereference this address-looking number to see if it is in fact containing the small structure value? Might get you closer to your answer. Commented Jun 1, 2013 at 8:13
  • Thanks Ryan. Can you be more precise ? You mean I don't put "&" before the parameter "bigStructure" ? Commented Jun 1, 2013 at 9:09

2 Answers 2

1

Seems to me that the problem is with getLinkedListElement(). Here's some code suggestion :

  BigStructure* getLinkedListElement(BigStructure** linkedList, int id)
    {
        int i;
        if( linkedList == NULL || *linkedList == NULL)
        return NULL ; 

     //We cannot update HEAD(linkedList), therfore using local pointer.
        BigStructure* linkWalk = * linkedList;

    /*I am asuming ids are mapped to linked list nodes as below.
     id 0 -> first node
     id 1 -> second node
     ......
     id n -> n-1 node
    */

//starting from second node since linkWalk is already pointing to first above.
        for(i=1; i<id && linkWalk != NULL; i++)  
                linkWalk = linkWalk->next;

   // At this point , either id = 0 OR id = i OR the link has been traversed.

       return linkWalk ;

    }

Lastly , before calling g_print("%d", tmpBigStructure->id) , Please check tmpBigStructure != NULL.

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

1 Comment

Yesss it works !! Thank you very much :) You should become a member of Stackoverflow to help people :)
0

Seems to me that the global : "BigStructure* bigStructure = NULL" is your head pointer and you passed its address.You can add nodes at the head.Therefore , you don't need to walk down the list to add a link node.

Suggestion : you are passing the address of bigStructure( i.e a double pointer) and operating on it as a single pointer in structure manipulation functions. This needs to be changed in all your functions.

As an example, your function to add nodes could be like(Assuming linkedList is HEAD) :

BigStructure* addLinkedListElement(BigStructure** linkedList)
{
    BigStructure* newLinkedList = malloc(sizeof(BigStructure));
    if (newLinkedList == NULL)
      return NULL ;    // Just to handle malloc failure

    newLinkedList->next = *linkedList;
    *linkedList = newLinkedList;
    return newLinkedList;
}

1 Comment

Thank you user2442730. Well this code works for the big structure but when I want to add a small structure I can't have right values and the program crashes. Can you give me the right implementation of " void addSmallStucture(UINT x, UINT y)" ? Thank you very much.

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.