Я пишу программу, которая добавляет символ в список. Проблема в том, что когда у меня есть свой первоначальный список из 5 узлов, и я пытаюсь добавить еще один, все работает, но когда я пытаюсь добавить еще один, функция stampa становится бесконечной.
Я не могу найти ошибку , Любая помощь будет приветствоваться. Вот код:
struct nodo {
char c;
struct nodo *next;
};
typedef struct nodo *lista;
void mc_coda(lista *l, char el);
void stampa(lista nodo01);
int main() {
int test[5] = {0, 1, 1, 0, 1};
lista nodo01 = malloc(sizeof(struct nodo));
lista nodo02 = malloc(sizeof(struct nodo));
lista nodo03 = malloc(sizeof(struct nodo));
lista nodo04 = malloc(sizeof(struct nodo));
lista nodo05 = malloc(sizeof(struct nodo));
(*nodo01).next = nodo02;
(*nodo02).next = nodo03;
(*nodo03).next = nodo04;
(*nodo04).next = nodo05;
(*nodo05).next = NULL;
(*nodo01).c = 'a';
(*nodo02).c = 'b';
(*nodo03).c = 'c';
(*nodo04).c = 'd';
(*nodo05).c = 'e';
mc_coda(&nodo01, 'e');
mc_coda(&nodo01,
'f'); // If this line is removed everything workd fine, if this line
// if added that it prints infinite f
stampa(nodo01);
return 0;
}
void mc_coda(lista *l, char el) {
if ((*l) == NULL) {
struct nodo temp;
temp.next = NULL;
temp.c = el;
(*l) = &temp;
} else {
while ((*l) != NULL) {
l = &(*(*l)).next;
}
struct nodo temp;
temp.next = NULL;
temp.c = el;
(*l) = &temp;
}
}
void stampa(lista nodo01) {
while (nodo01 != NULL) {
printf("%c\n", (*nodo01).c);
nodo01 = (*nodo01).next;
}
}