【问题标题】:Access to linked list variables inside a linked list访问链表内的链表变量
【发布时间】:2013-05-31 20:13:14
【问题描述】:

我有一个包含另一个链表的链表,我想在其中集成数据,但我做不到。

这是我的代码:

结构声明:

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

【问题讨论】:

  • 为什么当你添加一个链表元素时,如果下一个元素为NULL,你会返回?你不应该沿着列表走下去直到下一个值为NULL,然后在下一个指针指向的地方放置一个新的链表元素吗?
  • 我更正了,谢谢!我现在仍然对“子”链接列表有问题。
  • 明天我会添加回复;好好想办法。
  • 非常感谢 Magn3s1um。
  • 只是一个调试注释:您是否尝试和取消引用这个看起来像地址的数字以查看它是否实际上包含小结构值?可能会让你更接近你的答案。

标签: c pointers data-structures linked-list


【解决方案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。

【讨论】:

  • 是的,它有效!非常感谢 :) 你应该成为 Stackoverflow 的成员来帮助人们:)
【解决方案2】:

在我看来,全局:“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;
}

【讨论】:

  • 谢谢 user2442730。好吧,这段代码适用于大结构,但是当我想添加一个小结构时,我无法获得正确的值并且程序崩溃。你能给我正确的“void addSmallStucture(UINT x, UINT y)”实现吗?非常感谢。
猜你喜欢
  • 1970-01-01
  • 2015-03-05
  • 1970-01-01
  • 2016-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多