Untitled
/* Scrivere un sottoprogramma che riceve come parametro una lista di numeri interi detta l1. Il sottoprogramma costruisce e restituisce una nuova lista l2 contenente tutti i valori interi non presenti in l1 compresi tra il valore minimo ed il valore massimo di l1, ordinati in ordine crescente. Esempio 1 Ricevuta in ingresso la lista l1 -> 4 -> 1 -> -5 -> 2 -> 0 il sottoprogramma restituirà la lista l2 -> -4 -> -3 -> -2 -> -1 -> 3 Esempio 2 Ricevuta in ingresso la lista l1 -> 5 -> 4 il sottoprogramma restituirà una lista l2 vuota Si considerino già disponibili e non da sviluppare la definizione di tipo struttura ed i sottoprogrammi seguenti: typedef struct nodo_ { int n; struct nodo_* next; } nodo_t; /* inserisce l'elemento n in testa alla lista l e restituisce la testa della lista modificata*/ nodo_t* inserisciTesta(nodo_t* l, int n); /* inserisce l'elemento n in coda alla lista l e restituisce la testa della lista modificata*/ nodo_t* inserisciCoda(nodo_t* l, int n); /* elimina la testa della lista l e restituisce la testa della lista modificata*/ nodo_t* eliminaTesta(nodo_t* l); /* elimina la coda della lista l e restituisce la testa della lista modificata*/ nodo_t* eliminaCoda(nodo_t* l); /* elimina l'elemento in posizione pos nella lista l, se esiste, e restituisce la testa della lista modificata*/ nodo_t* elimina(nodo_t* l, int pos); /* retrituisce 1 se l'elemento n è presente nella lista l, altrimenti 0*/ int esiste(nodo_t* l, int n); /* visualizza il contenuto della lista l */ void visualizza(nodo_t* l); */ #include <stdio.h> int esiste (nodo_t *l, int i) { nodo_t *curr = l; while (curr) { if(curr->n == i){ return 1; } curr = curr -> next; } return 0; } //l1 -> 4 -> 1 -> - 5 -> 2 -> 0 --> NULL nodo_t *numeriMancanti(nodo_t *l1){ nodo_t *l2 = NULL; int min, max; min = max = l1 -> n; nodo_t* curr = l1; while(curr != NULL){ if(curr->n > max){ max = curr->n; } if(curr->n < min){ min = curr->n; } curr = curr->next; } if(max-min <= 1){ return NULL; } for(int i = min +1; i< max; i++) { if(esiste(l1, i) != 1){ l2 = inserisciCoda(l2, i); } } return l2; } int main(int argc, char *argv[]) { return 0; }
Leave a Comment