Marvee Amasi
Marvee Amasi
DIIDevHeads IoT Integration Server
Created by Marvee Amasi on 5/13/2024 in #middleware-and-os
disturbing issue I'm having with LinkedList in C language, when I count how many nodes should be
Hello world!! @Middleware & OS There is this disturbing issue I'm having with LinkedList in C language, when I count how many nodes should be in the list, I always get 1
LL count: 1
LL count: 1
Here is the relevant code to get the last element , to add, and to count
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 countLL(LL * head)
{
CPU_INT32U elements = 0;
LL * temp = head;
while(temp->next != DEF_NULL)
{
temp = temp->next;
elements++;
}
return elements;
}
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 countLL(LL * head)
{
CPU_INT32U elements = 0;
LL * temp = head;
while(temp->next != DEF_NULL)
{
temp = temp->next;
elements++;
}
return elements;
}
So here is how I call the function :
LL* list;

addLL(list);
int count = countLL(list);
Debug_LOG("LL count: %i", count);
LL* list;

addLL(list);
int count = countLL(list);
Debug_LOG("LL count: %i", count);
Please help me spot where I went wrong?
13 replies