I've written this function:
struct nodo * MinimoPariListaRic(struct nodo * top) {
struct nodo * minimo_p = NULL; //this should be the minimum even number
//list have one or no one element
if(top) {
if(!top->next)
if(top->valore % 2 == 0) return top; // valore(italian) = value
else return NULL;
}
else return NULL;
if(top->next)
minimo_p = MinimoPariListaRic(top->next);
if (top->valore % 2 == 0)
{
if(minimo_p->valore < top->valore) return minimo_p;
else return top;
}
else return minimo_p;
}
If the list's elements are all even, the function will return the minimum, and it's ok. But if an odd number appears, the function doesn't work correctly. If all the numbers are odd, function should return NULL.
your algorithm is wrong, you can have an undefined behavior if the recursive call return NULL because setting minimo_p to NULL and you will dereference it if (top->valore % 2 == 0)
, and it is too much complicated, you do not need a recursive call, just to go throw the list, for instance :
struct nodo * MinimoPariListaRic(struct nodo * l) {
struct nodo * minimo_p = NULL; //this should be the minium even number
while (l != NULL) {
if (l->valore % 2 == 0) {
if ((minimo_p == NULL) || (l->valore < minimo_p->valore))
minimo_p = l;
}
l = l->next;
}
return minimo_p;
}
Because your edited topic requires a recursive call you can do for instance :
struct nodo * MinimoPariListaRic(struct nodo * l) {
if (l == NULL)
return NULL;
else {
struct nodo * minimo_p = MinimoPariListaRic(l->next);
return ((l->valore % 2 == 0) &&
((minimo_p == NULL) || (l->valore < minimo_p->valore)))
? l : minimo_p;
}
}
as you can see this is simple, I check only one times in the program if valore is odd or not etc
using a full program to check :
#include <stdio.h>
#include <stdlib.h>
struct nodo {
struct nodo * next;
int valore;
};
struct nodo * MinimoPariListaRic(struct nodo * l) {
if (l == NULL)
return NULL;
else {
struct nodo * minimo_p = MinimoPariListaRic(l->next);
return ((l->valore % 2 == 0) &&
((minimo_p == NULL) || (l->valore < minimo_p->valore)))
? l : minimo_p;
}
}
int main(int argc, char ** argv)
{
/* make a list from the args */
struct nodo * l = NULL;
while (argc > 1) {
struct nodo * r = malloc(sizeof(*r));
r->next = l;
if (sscanf(argv[--argc], "%d", &r->valore) != 1) {
puts("invalid number");
return 0;
}
l = r;
}
/* show list well made */
struct nodo * ll;
for (ll = l; ll != NULL; ll = ll->next)
printf("%d ", ll->valore);
putchar('\n');
/* */
ll = MinimoPariListaRic(l);
if (ll == NULL)
puts("no even value");
else
printf("min even value is %d\n", ll->valore);
/* free resources */
while (l) {
ll = l->next;
free(l);
l = ll;
}
return 0;
}
Compilation and executions:
pi@raspberrypi:/tmp $ gcc -Wall l.c
pi@raspberrypi:/tmp $ ./a.out 1 4 2 7 8
1 4 2 7 8
min even value is 2
pi@raspberrypi:/tmp $ ./a.out
no even value
pi@raspberrypi:/tmp $ ./a.out 3
3
no even value
pi@raspberrypi:/tmp $ ./a.out 3 3 1 5
3 3 1 5
no even value
pi@raspberrypi:/tmp $ valgrind ./a.out 3 4 5 2 0
==16881== Memcheck, a memory error detector
==16881== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==16881== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==16881== Command: ./a.out 3 4 5 2 0
==16881==
3 4 5 2 0
min even value is 0
==16881==
==16881== HEAP SUMMARY:
==16881== in use at exit: 0 bytes in 0 blocks
==16881== total heap usage: 6 allocs, 6 frees, 1,064 bytes allocated
==16881==
==16881== All heap blocks were freed -- no leaks are possible
==16881==
==16881== For lists of detected and suppressed errors, rerun with: -s
==16881== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
pi@raspberrypi:/tmp $