我们与在C. LinkedList的一个问题,当我应该算多少节点出现在列表中,我总是得到1LinkedList的添加元素
LL数:1
这是添加,计数和获得的代码列表最后一个元素:
void addLL(LL * head)
{
LL *newNode;
LL *tail = getLastNode(head);
newNode = malloc(sizeof(LL));
if(newNode != DEF_NULL)
{
newNode->ID=-1;
newNode->TCB=-1;
newNode->next = DEF_NULL;
if(!head) head = newNode;
else tail->next = newNode;
}
}
LL * getLastNode(LL * head)
{
LL *temp = head;
while(temp->next != DEF_NULL)
{
temp = temp->next;
}
return temp;
}
CPU_INT32U countLL(LL * head)
{
CPU_INT32U elements = 0;
LL * temp = head;
while(temp->next != DEF_NULL)
{
temp = temp->next;
elements++;
}
return elements;
}
这就是所谓的以这样的方式
addLL(list);
temp = countLL(list);
Debug_LOG("LL count: %i", temp);
其中LL * list;是一个全局变量,temp在本地范围内。 我希望每个人都可以看到我错在哪里
问候, Sjaak和格里特
你确定该列表不为空值,则与newNode = malloc的添加仅一个元素(的sizeof(LL)); – subbul 2012-04-25 10:27:49
这里有什么问题?如果你添加一个元素,计数将只是一个元素...添加更多的元素来测试你的'countLL()' – 2012-04-25 10:30:15
对不起,我应该说每一秒都会在主程序中调用AddLL。 – Davey 2012-04-25 10:31:32