Il testo: Scrivere una funzione ricorsiva che data una lista concatenata di interi , restituisca una nuova lista costituita dagli elementi di posto dispari
il mio codice :
codice:
//ritorna lista con gli elementi di posto dispari
#include<stdio.h>
#include<stdlib.h>
struct elemento{
int inf;
struct elemento*next;
};
struct elemento* disp_lista(struct elemento*,int);
struct elemento* crea_lista(int);
int main(void){
struct elemento*testa,*primo_disp=NULL;
int dim,i=1;
printf("Inserisci gli elementi della lista\n");
scanf("%d",&dim);
testa=crea_lista(dim);
primo_disp=disp_lista(testa,i);
while(primo_disp!=NULL){
printf("%d",primo_disp->inf);
printf("->");
primo_disp=primo_disp->next;
}
printf("NULL\n");
system("pause");
return 0;
}
struct elemento*crea_lista(int dim){
struct elemento *testa,*curr,*aus;
int i=0;
testa=(struct elemento*)malloc(sizeof(struct elemento));
printf("inserisci elemento %d\n",i+1);
scanf("%d",&testa->inf);
testa->next=NULL;
curr=testa;
for(i=1;i<dim;i++){
aus=(struct elemento*)malloc(sizeof(struct elemento));
printf("inserisci elemento %d\n",i+1);
scanf("%d",&aus->inf);
curr->next=aus;
curr=aus;
aus->next=NULL;
}
return testa;
}
struct elemento*disp_lista(struct elemento*curr,int i){
struct elemento*aus=NULL,*primo=NULL;
if(curr==NULL) return NULL;
else{
if(i%2!=0){
aus=(struct elemento*)malloc(sizeof(struct elemento));
aus=curr;
aus->next=disp_lista(curr->next,i++);
return aus;
}
else
aus=disp_lista(curr->next,i++);
}
}
e giustamente l'errore
: il problema è che nella funzione non incrementa la variabile i e nella condizione dell'else non entra di conseguenza