我们在C中的LinkedList有问题。 当我计算列表中应该有多少个节点时,我总是得到1
LL数:1
这是列表代码的Add,count和get last元素:
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 *列表;是一个全局变量,temp在本地范围内。 我希望任何人都能看到我出错的地方
问候, Sjaak和Gerrit
答案 0 :(得分:0)
在Windows上,这个功能没什么问题 - 奇怪......
ideone also shows good output.
#include <stdio.h>
#include <stdlib.h>
typedef struct LL{
struct LL *next;
}LL;
LL * getLastNode(LL * head)
{
LL *temp = head;
while(temp->next != NULL)
{
temp = temp->next;
}
return temp;
}
void addLL(LL * head)
{
LL *newNode;
LL *tail = getLastNode(head);
newNode = malloc(sizeof(LL));
if(newNode != NULL)
{
newNode->next = NULL;
if(!head) head = newNode;
else tail->next = newNode;
}
}
int countLL(LL * head)
{
int elements = 0;
LL * temp = head;
while(temp->next != NULL)
{
temp = temp->next;
elements++;
}
return elements;
}
int main() {
LL *h = malloc(sizeof(*h));
addLL(h);
addLL(h);
addLL(h);
printf("%d\n", countLL(h)); // prints 3 as expected
}
答案 1 :(得分:0)
CPU_INT32U countLL(LL * head){CPU_INT32U elements = 0; LL * temp = head; while(temp-&gt; next!= DEF_NULL){temp = temp-&gt; next; elements ++;} return elements;} < / p>
在此函数中,您将元素变量声明为auto 因此,一旦函数退出,它的存储就会被解除分配,因为内存现在可以自由地分配给不同的变量,因此可能会被覆盖,因此之前的cvalue会丢失
所以为了避免这种情况,请在声明变量时使用static ..... 因为静态变量内存只有在执行完整个程序后才会被释放 请试试......
答案 2 :(得分:0)
void addLL(LL * head)
{
LL *newNode;
LL *tail = getLastNode(head);
这里有一个问题,如果(全局)头碰巧为NULL,它将被getLastNode()函数取消引用:
LL * getLastNode(LL * head)
{
LL *temp = head;
while(temp->next != DEF_NULL)
此处temp->next != ...
将导致temp被解除引用。如果temp恰好为NULL,那将导致NULL指针解除引用。 (如插入函数的调用。你可以添加一个额外的测试(或使用更清洁的指针指针):
while(temp && temp->next != DEF_NULL)
更新(显示指向指针版本的指针更清晰)
#include <stdlib.h>
#include <stdio.h>
#define DEF_NULL NULL
#define CPU_INT32U unsigned
typedef struct link {
struct link *next;
} LL;
LL *globhead=NULL;
LL **getTailPP(LL **ppHead);
CPU_INT32U countLL(LL * ptr);
void addLL(LL **ppHead);
void addLL(LL **ppHead)
{
ppHead = getTailPP(ppHead);
*ppHead = malloc(sizeof **ppHead);
if(*ppHead != DEF_NULL)
{
// newNode->ID=-1;
// newNode->TCB=-1;
(*ppHead)->next = DEF_NULL;
}
}
LL **getTailPP(LL **ppHead)
{
for( ; *ppHead; ppHead = &(*ppHead)->next ) {;}
return ppHead;
}
CPU_INT32U countLL(LL * ptr)
{
CPU_INT32U elements = 0;
for(; ptr != DEF_NULL; ptr=ptr->next) { elements++; }
return elements;
}
int main()
{
unsigned count;
addLL( &globhead);
count = countLL (globhead);
printf("count = %u\n", count);
addLL( &globhead);
count = countLL (globhead);
printf("count = %u\n", count);
return 0;
}
答案 3 :(得分:0)
我在您的代码中看到了几个问题:
head
但修改不会在该功能之外传播。 你应该传递一个“指向列表指针的指针”(即LL**
),它等同于“LL*
的地址”;了解我如何致电addLL()
以及我如何修改其原型和head
作业我建议修改后的代码适用于1,2或任何列表长度(我刚刚将CPU_INT32U
更改为int
以使用MinGW快速编译,我可以使用typedef'ined):< / p>
#include <stdio.h>
#define DEF_NULL 0
typedef struct tagL {
int ID;
int TCB;
struct tagL *next;
} LL;
void addLL(LL ** head);
LL * getLastNode(LL * head);
int countLL(LL * head);
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;
if (head){
while(temp->next != DEF_NULL)
{
temp = temp->next;
}
}
return temp;
}
int countLL(LL * head)
{
int elements = 0;
LL * temp = head;
if (head){
do {
temp = temp->next;
elements++;
} while(temp != DEF_NULL);
}
return elements;
}
int main(int argc, char *argv[]){
LL *list = 0;
printf("LL test\n");
addLL(&list);
printf("length = %d\n", countLL(list));
addLL(&list);
printf("length = %d\n", countLL(list));
addLL(&list);
printf("length = %d\n", countLL(list));
}
输出:
LL test
length = 1
length = 2
length = 3