I've basically just begun studying lists, and I ran into problems in the first exercises. This is a program to print the elements of the list.
#include <stdio.h>
#include <stdlib.h>
struct EL {
int info;
struct EL *next;
};
typedef struct EL ElementoLista;
typedef ElementoLista *ListaDiElementi;
void InserisciTestaLista(ListaDiElementi *lista, int elem){
ListaDiElementi aux;
aux=malloc(sizeof(ElementoLista));
aux->info=elem;
aux->next=*lista;
*lista=aux;
};
void readListIntRic (ListaDiElementi *L, int K ) {
int x;
if (K>0){
printf("Digita un intero elemento della lista\n");
scanf("%d",&x);
InserisciTestaLista(L,x);
readListIntRic(L,K-1);
}
};
void stampaLista (ListaDiElementi lista){
while (lista != NULL){
printf("%d->",lista->info);
lista=lista->next;
}
printf("//");
};
int main(){
ListaDiElementi lista;
int k;
printf("Inserisci il numero di elementi di cui vuoi che la lista sia costituita\n");
scanf("%d",&k);
readListIntRic(&lista,k);
stampaLista(lista);
}
The program behaves strangely:
-It prints 0 at the end, even though there is no zero in the list;
-I put the printf("//");
just to see if the program exited the cycle. Turns out, it doesn't.
This is giving me a lot of problems. I've been trying to do other exercises as well, but they all have the same problem and I can't figure out what it is. I'm using the GCC compiler with wxDev-C+