I am new to C. Is there different memory management in both cases like the first one stores data in the stack and the second one in heap. In the first case, is the memory allocated by the compiler. Which one is better to use?
FIRST CODE
#include <stdio.h>
#include <stdlib.h>
struct Node{
int data;
struct Node* next;
};
void printLL(struct Node* head){
printf("%d ", (*head).data);
if((*head).next!=NULL){
printLL((*head).next);
}
}
int main()
{
struct Node first, second, third;
first.data = 9;
first.next = &second;
second.data = 3;
second.next = &third;
third.data = 7;
third.next = NULL;
printLL(&first);
return 0;
}
SECOND CODE
#include <stdio.h>
#include <stdlib.h>
struct Node{
int data;
struct Node* next;
};
void printLL(struct Node* head){
printf("%d ", (*head).data);
if((*head).next!=NULL){
printLL((*head).next);
}
}
int main()
{
struct Node* first;
struct Node* second;
struct Node* third;
first = (struct Node*)malloc(sizeof(struct Node));
second = (struct Node*)malloc(sizeof(struct Node));
third = (struct Node*)malloc(sizeof(struct Node));
first->data = 9;
first->next = second;
second->data = 3;
second->next = third;
third->data = 7;
third->next = NULL;
printLL(first);
return 0;
}
if there are any resources to read from pls send the link