Программа компилируется, но при запуске появляется ошибка "Прекращена работа программы"
Только начала погружаться в эту тему и никак не могу понять, в чём ошибка :(
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
int value;
struct Node *next;
} node_t;
typedef struct List
{
node_t *head;
int size;
} list_t;
void
init (list_t *l)
{
l->head = NULL;
l->size = 0;
}
void
push_back (list_t *l, int value)
{
node_t *cur = l->head;
while (cur->next != NULL) cur = cur->next;
node_t *tmp = (node_t*) malloc (sizeof (node_t));
tmp->value = value;
tmp->next = NULL;
cur->next = tmp;
l->size++;
}
void
print_list (list_t *l)
{
node_t *cur = l->head;
while (cur != NULL)
{
printf("%d", cur->value);
cur = cur->next;
}
}
void
destroy (list_t *l)
{
node_t *cur = l->head;
node_t *tmp;
while (cur != NULL)
{
tmp = cur;
cur = cur->next;
free (tmp);
}
}
int
main ()
{
int x = 1, y = 2;
list_t *l = NULL;
init (l);
push_back (l, x);
push_back (l, y);
print_list (l);
destroy (l);
return 0;
}
init
передается null-указатель, а затем через этот null-указатель что-то пытаются писать. Ну все и падает. Что вы хотели этим сказать?list_t *l = (list_t*) malloc (sizeof (list_t));