I'm wring my linked list implementation in C and I am facing the problem that linked list doest not add elements to itself. It only prints the first element.
Something is wrong here and I can't figure out what.
I managed to localize the problem that first element next field is always NULL no matter what I do. All the other things are legal. I add the results of the running at the end.
Here is linked list declaration:
typedef struct list_element {
int value;
struct list_element *next;
} list_element;
Here is usage:
void checkLinkedList()
{
list_element list = createNewLinkedList();
list.value = 100;
for (int i = 1; i < 10; i++) {
printf("added another\n");
int value = rand();
insertNewElementAtEndWithValue(list, value);
}
}
void insertNewElementAtEndWithValue(list_element element, int value)
{
printf("Starting element address: %p\n", &element);
list_element *myElement = &element;
int shouldContinue = 1;
while (shouldContinue) {
if (myElement->next == NULL) {
printf("next is null, value is %d \n", value);
list_element *new = (list_element *)malloc(sizeof(list_element));
printf("new address %p\n", new);
new->value = value;
new->next = NULL;
myElement->next = new;
printf("New is: %p, myElement->next is: %p\n", new, myElement->next);
shouldContinue = 0;
} else {
printf("Proceeding to next");
myElement = myElement->next;
}
}
}
Results:
./main
added another
Starting element address: 0x7fff5c10aac0
next is null, value is 16807
new address 0x7fa3d3504b60
New is: 0x7fa3d3504b60, myElement->next is: 0x7fa3d3504b60
added another
Starting element address: 0x7fff5c10aac0
next is null, value is 282475249
new address 0x7fa3d3504b70
New is: 0x7fa3d3504b70, myElement->next is: 0x7fa3d3504b70
added another
Starting element address: 0x7fff5c10aac0
next is null, value is 1622650073
new address 0x7fa3d3504b80
New is: 0x7fa3d3504b80, myElement->next is: 0x7fa3d3504b80
added another
Starting element address: 0x7fff5c10aac0
next is null, value is 984943658
new address 0x7fa3d3504b90
New is: 0x7fa3d3504b90, myElement->next is: 0x7fa3d3504b90
added another
Starting element address: 0x7fff5c10aac0
next is null, value is 1144108930
new address 0x7fa3d3504ba0
New is: 0x7fa3d3504ba0, myElement->next is: 0x7fa3d3504ba0
added another
Starting element address: 0x7fff5c10aac0
next is null, value is 470211272
new address 0x7fa3d3504bb0
New is: 0x7fa3d3504bb0, myElement->next is: 0x7fa3d3504bb0
added another
Starting element address: 0x7fff5c10aac0
next is null, value is 101027544
new address 0x7fa3d3504bc0
New is: 0x7fa3d3504bc0, myElement->next is: 0x7fa3d3504bc0
added another
Starting element address: 0x7fff5c10aac0
next is null, value is 1457850878
new address 0x7fa3d3504bd0
New is: 0x7fa3d3504bd0, myElement->next is: 0x7fa3d3504bd0
added another
Starting element address: 0x7fff5c10aac0
next is null, value is 1458777923
new address 0x7fa3d3504be0
New is: 0x7fa3d3504be0, myElement->next is: 0x7fa3d3504be0
Number of elements in list: 1
100,