I kept trying but I get this message 'warning: incompatible pointer to integer conversion assigning to 'int' from 'void *' [-Wint-conversion] node2->data = NULL;
I got this code from the lecture so I'm sure that some(or most of) compilers or computers are okay with compiling this code, but mine doesn't work. I use mac os and visual studio code, and it seems that NULL causes Segmentation fault: 11 in my case. How am I supposed to fix this?
#include <stdio.h>
#include <stdlib.h> // malloc
typedef struct Node
{
struct Node *next;
int data;
} Node;
Node *head;
void freeAll(Node *root)
{
Node *cur = head->next;
while(cur != NULL)
{
Node *next = cur->next;
free(cur);
cur = next;
}
}
int main(void)
{
head = (Node *)malloc(sizeof(Node));
Node *node1 = (Node *)malloc(sizeof(Node));
node1->data = 1;
Node *node2 = (Node *)malloc(sizeof(Node));
node2->data = 2;
head->next = node1;
node1->next = node2;
node2->data = NULL;
Node *cur = head->next;
while (cur != NULL)
{
printf("%d ", cur->data);
cur = cur->next;
}
return 0;
}
In this statement
node2->data = NULL;
there is a typo. There shall be
node2->next = NULL;
Also you forgot to initialize the data member data
of the head
node.
head = (Node *)malloc(sizeof(Node));
//...
head->next = node1;
It is not a good approach to define a dummy head node.
It would be much better to write a function that inserts new nodes in the list instead of "manually" to create nodes.
Pay attention to that the function freeAll
can invoke undefined behavior because in general the pointer head
can be equal to NULL
. In this case there is an attempt to use a null pointer to access memory in this statement.
Node *cur = head->next;
Also the parameter root
is not used in the function. If your list is based on the global variable head
(that is a bad idea) then the function can be declared and defined the following way
void freeAll( void )
{
while( head != NULL )
{
Node *tmp = head;
head = head->next;
free( tmp );
}
}
A more general function that does not depend on a global variable can be defined the following way
void freeAll( Node **head )
{
while( *head != NULL )
{
Node *tmp = *head;
*head = ( *head )->next;
free( tmp );
}
}
and called like
freeAll( &head );
Here is a demonstrative program that shows how the list can be defined.
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
struct Node *next;
int data;
} Node;
int push_back( Node **head, int data )
{
Node *new_node = malloc( sizeof( Node ) );
int success = new_node != NULL;
if ( success )
{
new_node->next = NULL;
new_node->data = data;
while ( *head ) head = &( *head )->next;
*head = new_node;
}
return success;
}
void freeAll( Node **head )
{
while ( *head )
{
Node *tmp = *head;
*head = ( *head )->next;
free( tmp );
}
}
void display( const Node *head )
{
for ( ; head != NULL; head = head->next )
{
printf( "%d -> ", head->data );
}
puts( "null" );
}
int main(void)
{
Node *head = NULL;
const int N = 10;
for ( int i = 0; i < N; i++ )
{
push_back( &head, i + 1 );
}
display( head );
freeAll( &head );
return 0;
}
The program output is
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> null