Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- typedef struct nodo {
- int valore;
- struct nodo *succ;
- } nodo_t;
- nodo_t *crea_lista();
- void stampa_lista(nodo_t *);
- int cerca_numero(nodo_t *);
- int validazione_input();
- int main() {
- nodo_t *testa; /*variabile che punterà alla lista*/
- int trovato; /*variabile risultato*/
- srand(time(NULL));
- testa = crea_lista();
- stampa_lista(testa);
- trovato = cerca_numero(testa);
- if (trovato)
- printf("Trovato!\n");
- else
- printf("Non trovato!\n");
- return(0);
- }
- nodo_t *crea_lista() {
- nodo_t *p , *succ_p;
- int dim = rand() % 10 + 1;
- p = (nodo_t *)malloc(sizeof(nodo_t));
- p->valore = rand() % 100 + 1;
- p->succ = NULL;
- succ_p = p;
- for(int i = 2 ; i <= dim ; i++) {
- succ_p->succ = (nodo_t *)malloc(sizeof(nodo_t));
- succ_p = succ_p->succ;
- succ_p->valore = rand () % 100 + 1;
- succ_p->succ = NULL;
- }
- return(p);
- }
- void stampa_lista(nodo_t *testa) {
- nodo_t *p = testa;
- printf("Lista: \n");
- while (p != NULL) {
- printf("%d\t" , p->valore);
- p = p->succ;
- }
- return;
- }
- int cerca_numero(nodo_t *testa) {
- int ris, n;
- nodo_t *p = testa;
- printf("\nDigita il numero da cercare:\n");
- n = validazione_input();
- while(p != NULL && ris != 1) {
- ris = (p->valore == n)? 1 : 0;
- p = p->succ;
- }
- return(ris);
- }
- int validazione_input() {
- int numero, controllo, pulizia_buffer;
- do {
- controllo = scanf("%d", &numero);
- if (!controllo) {
- printf("Input non accettabile, prego reinserire!\n");
- while ((pulizia_buffer = getchar()) != '\n');
- }
- }
- while (!controllo);
- while ((pulizia_buffer = getchar()) != '\n');
- return(numero);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement