Hello, I started this week to deal a little bit with linked lists, I did a simple example, I'm not sure the way I did it is correct, maybe I'm missing something, here's the code:
Code:
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next; //wohhaaa... a linked list :)
};
void add( struct node **head, int data ) {
struct node *tmp = malloc( sizeof ( struct node ) );
tmp->data = data;
tmp->next = *head;
*head = tmp;
}
void print( struct node *head ) {
struct node *current = head;
while( current ) {
printf( "data: %d\n", current->data );
current = current->next;
}
}
void collect( struct node *head ) {
struct node *current = head;
while(current ) {
free( current );
current = current->next;
}
}
int main( void ) {
struct node *head = NULL;
add( &head, 4 );
add( &head, 3 );
add( &head, 2 );
print( head );
collect( head );
return 0;
}
My big question is, my collect() function really works? it's correct, or I'm forgetting to free something?