2013-05-31 119 views
2

我有一個鏈接列表與其中的另一個鏈接列表,我想在它們中集成數據,但我不能。訪問鏈接列表中的鏈接列表變量

這裏是我的代碼:

結構聲明:

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; 

結構操作功能:

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

我想上面的代碼訪問SmallStructure變量,但我得到一個大號碼(看起來像一個地址):

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 
} 
} 
+0

如何在添加鏈表元素時返回,如果下一個元素爲NULL,則返回?你不應該在列表中走下去,直到下一個值爲NULL,然後在下一個指針指向的位置放置一個新的鏈接列表元素? – Magn3s1um

+0

我改正了,謝謝! 「兒童」鏈接列表現在仍然存在問題。 – MHDaouas

+1

明天添加回應;好了解這一點。 – Magn3s1um

回答

1

在我看來,這個問題是getLinkedListElement()。 下面是一些代碼的建議:

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 ; 

    } 

最後, 調用了g_print( 「%d」,tmpBigStructure-> ID)之前,請檢查tmpBigStructure!= NULL。

+0

Yesss它的作品!非常感謝:)你應該成爲Stackoverflow的成員來幫助人們:) – MHDaouas

0

在我看來,全球性的:「BigStructure * bigStructure = NULL」是您的頭部指針,並且您通過了它的地址。您可以在頭部添加節點。因此,您不需要沿着列表走下去添加鏈接節點。

建議: 您正在傳遞bigStructure的地址(即雙指針) ,並將其作爲結構操作函數中的單個指針進行操作。 這需要在您的所有功能中進行更改。

舉個例子,你的函數添加節點也能像(假設LinkedList的是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; 
} 
+0

謝謝user2442730。那麼這段代碼適用於大型結構,但是當我想添加一個小型結構時,我無法獲得正確的值和程序崩潰。你能給我正確的執行「 void addSmallStucture(UINT x,UINT y)」嗎?非常感謝你。 – MHDaouas