Skip to content
Snippets Groups Projects
Commit 08093476 authored by 20041679's avatar 20041679
Browse files

added Compito 27 luglio

parent 89ebc350
No related branches found
No related tags found
No related merge requests found
// Dalla consegna non si capisce niente, a partire dal fatto che
// si richiede n < m e nell'esempio n è 2 e m è 5.
// Inoltre cosa bisogna fare se i nodi non esistono tutti?
// E da che numero partono le posizioni?
// Gli estremi sono inclusi o esclusi?
//
// Questa implementazione richiede n <= m (si può eliminare
// anche un singolo elemento) e elimina i nodi se e solo se
// esistono tutti, le posizioni partono da 0 e gli estremi
// sono inclusi
void f(Link *list, size_t n, size_t m) {
size_t pos = 0;
// skip the first `n` nodes
while (*list && pos < n) {
list = &(*list)->next;
++pos;
}
if (*list) {
Link skip = *list;
pos = 0;
// traverse every node of the deletion area.
while (skip && pos < m - n) {
skip = skip->next;
++pos;
}
if (skip) {
// every node that needs to be deleted exists,
// proceed with deletion
skip = *list;
pos = 0;
while (skip && pos < m - n) {
Link next = skip->next;
free(skip);
skip = next;
++pos
}
// skip->next can be assumed to exist since this is the
// true branch of `if (skip)`;
// link the last node of the skipped area with the first
// node after the deleted area
*list = skip->next;
}
}
}
// first call: sum1 = 0, sum2 = 0
int f(Link list1, Link list2, int sum1, int sum2) {
if (list1 && list2) {
sum1 += list1->data;
sum2 += list2->data;
list1 = list1->next;
list2 = list2->next;
} else if (list1) {
sum1 += list1->data;
list1 = list1->next;
} else if (list2) {
sum2 += list2->data;
list2 = list2->next;
} else {
return sum1 > sum2
? sum1
: sum2;
}
return f(list1, list2, sum1, sum2);
}
// Complessità in tempo: O(max(|list1|, |list2|))
// Complessità in spazio: O(max(|list1|, |list2|))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment