Apologies to begin with if I am at the wrong forum, I know there are thousands of implementation of linkedlist already available on the internet, but I am looking at a place where I can discuss and see where am I going wrong with my specific implementation, so I can better my algorithmic thinking.
So I have just started learning DSA, and I am trying to implement a linkedlist. I have seen some tutorials and while the concept of linkedlist is fairly clear to me, I am facing problem with trying to implement a linkedlist.
I have written a piece of code according to my understanding of this data structure, but it doesn't seem to be working after the first element is added.
What I have tried:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next_node;
};
struct Node* head_ptr = NULL;
void addData(int val, struct Node* ptr_h) { struct Node* temp;
if(ptr_h == NULL) {
head_ptr = (struct Node*)malloc(sizeof(struct Node));
temp = head_ptr;
temp->data = val;
temp->next_node = NULL;
}
else if(ptr_h != NULL) {
temp = ptr_h;
while(temp != NULL) {
temp = temp->next_node;
}
temp = (struct Node*)malloc(sizeof(struct Node));
temp->data = val;
temp->next_node = NULL;
}
}
int main(int argc, char const *argv[])
{
addData(5, head_ptr);
addData(7, head_ptr);
addData(6, head_ptr);
addData(6, head_ptr);
int i = 0;
struct Node* address = head_ptr;
while(address != NULL) {
i++;
printf("%d\n", address->data);
printf("%p\n", address->next_node);
address = address->next_node;
}
printf("count is : %d\n", i);
return 0;
}
I have written a function named
addData()
whose purpose is to add elements. I have a head pointer
head_ptr
which I intend to always keep with me in order to have reference for the starting point of the linkedlist.
The logic is whenever
addData()
is called, it is checked whether the list is empty or is it already initialized (this is carried out by checking whether the head pointer references to NULL or not).
In the case if it references to NULL, some memory is dynamically allocated using
malloc()
and the
head_ptr
(which was initially assigned as
NULL
) is assigned this address. This is followed by assigning the data element of next node according to parameter passed through
addData()
, and the pointer element of the same next node is assigned as
NULL
signifying that the list ends there.
According to my understanding this much of code is working fine, because when I iterate through the list in the
main()
, it prints the first element.
I think the problem is in the following block of code, where if the
head_ptr
is not equal to
NULL
, a temporary variable
temp
is assigned its value and it is assigned the next address until it is not found to be
NULL
. After this, some memory is again dynamically allocate using
malloc()
, and its address is assigned to the
temp
variable, which is then used to set the data element of next node as the value passed through function parameter, and the pointer element of the same next node is set as NULL, signifying the list ends there.
In the
main()
, I am keeping a counter
i=0
, and iterating through the pointer, printing data and pointer element of each node. It prints the first element, but the pointer element points to
NULL
signifying that further elements once the first element is added was not added, and the variable
i
too is equal to 1 once the loop stops.
The output is as follows:
5
(nil)
count is : 1
I have gone through the code multiple times, but unable to understand where am I going wrong, because to me it seems perfectly logical. I am expecting a sort of discussion, where you folks can help me move in a direction where I can see where am I going wrong.