Ciao,
dovrei fare un programma che mi elimina un numero contenuto in una lista (se il numero si ripete più volte lo deve elinimare in tutte) e poi mi deve restituire la nuova lista.
Il programma che ho fatto non mi da errori nel compilatore però non esegue quello che dovrebbe fare.
Questo è il programma:
typedef struct NODO_LISTA {
int val;
struct NODO_LISTA *next;
} LISTA;
LISTA* CancellaLista(LISTA *l, int v)
{
LISTA *r,*s;
if ( v==l->val)
{
s=l;
l=(l)->next;
free(s);
}
while(l->next!=NULL)
{
if (v==l->next->val)
{
r=l->next;
l->next=l->next->next;
free(r);
}
else
l=l->next;
}
return l;
}
LISTA* insertListaTesta(LISTA *l, int v)
{
LISTA *t = (LISTA *) malloc(sizeof(LISTA));
if (t != NULL)
{
t->val = v;
t->next = l;
return t;
}
else
return l;
}
void stampaListaR(LISTA *l)
{
if (l != NULL)
{
printf("%d\n", l->val);
stampaListaR(l->next);
}
}
int main()
{
int x, i,y;
LISTA *p = NULL;
for (i = 0; i < 20; ++i)
{
x = rand() % 100;
p = insertListaTesta(p, x);
}
stampaListaR(p);
printf("\n");
scanf(" ",&y);
CancellaLista(p,y);
stampaListaR(p);
system("pause");
return 0;
}
Qualcuno riesce a capire dove ho sbagliato?
ciao