Soluzioni-Tutorato-01b

Anuncio
int sommaMul(Lista testa, int M) {
int accumulatore = -1;
while (testa != NULL) {
if (testa->dato % M == 0)
accumulatore += testa->dato;
testa = testa->next;
}
return accumulatore;
}
Lista max(Lista x) {
Lista m;
if (x != NULL) return x;
m
= max(x->next);
if (m != NULL && m->dato > x->dato)
return m->dato;
else
return x->dato;
}
int mediana(Lista testa) {
Lista Cursore1, Cursore2;
int piccoli, grandi;
if (testa == NULL)
return -1;
else {
for (Cursore1=testa; Cursore1!=NULL; Cursore1=Cursore1->next){
piccoli = grandi = 0;
for(Cursore2=testa;Cursore2!=NULL;Cursore2=Cursore2->next){
if (Cursore1 != Cursore2) {
if (Cursore2->dato < Cursore1->dato) ++piccoli;
if (Cursore2->dato > Cursore1->dato) ++grandi;
}
} // end for
if (piccoli == grandi) return Cursore1->dato;
} // end for
} // end else-if
return -1;
} // end mediana
int medianaA(Lista testa) {
Lista Cursore1, Cursore2;
int piccoli, grandi;
if (testa == NULL)
return -1;
else {
for (Cursore1=testa; Cursore1!=NULL; Cursore1=Cursore1->next) {
piccoli = grandi = 0;
for (Cursore2=testa; Cursore2!=NULL; Cursore2=Cursore2->next) {
if (Cursore1->dato != Cursore2->dato) {
if (Cursore2->dato < Cursore1->dato) ++piccoli;
if (Cursore2->dato > Cursore1->dato) ++grandi;
}
}
if (piccoli == grandi) return Cursore1->dato;
}
}
return -1;
}
ElementoLista* FirstEven(ListaDiInteri L) {
while (L != NULL) {
if (L->s % 2 == 0) return L;
L = L->next;
}
return L;
}
ElementoLista* MinEven(ListaDiInteri L) {
unsigned int primoPari = 1;
ElementoLista *minPtr = NULL;
while (L != NULL) {
if (L->s % 2 == 0) {
if (primoPari==1){ // primo valore pari incontrato!
minVal = L->s;
minPtr = L;
primoPari = 0;
} else if (L->s < minVal) {
minVal = L->s;
minPtr = L;
}
}
L = L->next;
}
return minPtr;
}
void foo(ListaDiTipo l, int el) {
unsigned int count = 1;
ElementoLista *curr = l, *prev=NULL;
while (curr != NULL && count <= 3) {
prev = curr;
curr = curr->next;
count++;
}
if (count == 4) {
prev->next = new ElementoLista;
prev->next->s = el;
prev->next->next = curr;
}
}
void foo2(ListaDiTipo l, int el) {
ElementoLista *ptr = NULL;
ElementoLista *aux;
while (l != NULL) {
if (l->s > el) maxPtr = l;
l = l->next;
}
if (maxPtr != NULL) {
aux = maxPtr->next;
maxPtr->next = new ElementoLista;
maxPtr->next->s = el;
maxPtr->next->next = aux;
}
}
void foo3(ListaDiTipo& l, int el) {
ElementoLista *curr = l;
ElementoLista *ptr = NULL;
ElementoLista *aux;
while (curr != NULL) {
if (curr->s > el) ptr = curr;
curr = curr->next;
}
if (ptr != NULL) {
aux = ptr->next;
ptr->next = new ElementoLista;
ptr->next->s = el;
ptr->next->next = aux;
} else {
aux = new ElementoLista;
aux->s = el;
aux->next = l;
l = aux;
}
}
void Eliminaiel(ListaDiInteri& testa, int i) {
while (i > 0 && testa != NULL) {
i = i - 1;
aux = testa;
testa = testa->next;
delete aux;
}
}
void fooUltimo(ListaDiInteri& L, int el) {
ElementoLista *curr = L, *prev = NULL;
unsigned int nodoInserito = 0;
while (curr != NULL && nodoInserito == 0) {
if (prev != NULL && prev->s < el && curr->s > el) {
prev->next = new ElementoLista;
prev->next->s = el;
prev->next->next = curr;
nodoInserito = 1;
}
prev = curr;
curr = curr->next;
}
if (nodoInserito == 0) {
curr = new ElementoLista;
curr->s = el;
curr->next = L;
L = curr
}
}
Descargar