Advertisement
Guest User

Untitled

a guest
Jul 15th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.41 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. typedef struct nodo {
  6.     int info;
  7.     struct nodo *next;
  8. } t_nodo;
  9.  
  10. t_nodo *crea_lista (t_nodo *head);
  11. void stampa_lista (t_nodo *head);
  12. void trova_elemento (t_nodo *head, int numb);
  13.  
  14. int main () {
  15.     t_nodo *head;
  16.     int num;
  17.         printf("La lista e' la seguente:\n");
  18.         head = (t_nodo*)malloc(sizeof(t_nodo));
  19.         head = crea_lista (head);
  20.         stampa_lista (head);
  21.         printf("\nChe numero vuoi cercare?\n");
  22.         do {
  23.             scanf("%d", &num);
  24.                 if (num <=0) printf("Il numero deve essere <= di 0\n");
  25.         }
  26.         while (num <= 0);
  27.         trova_elemento (head, num);
  28. return(0);
  29. }
  30.  
  31. t_nodo *crea_lista (t_nodo *head) {
  32.     t_nodo *p , *p_next;
  33.     int i=1, n_celle;
  34.     srand(time(NULL));
  35.         n_celle = rand() % 10 + 1;
  36.         head->info = rand() % 50 + 1;
  37.         p = head;
  38.         p_next = head;
  39.         p_next->next = NULL;
  40.             for (i=2; n_celle!=1 && i<=n_celle; i++) {
  41.                 p_next->next = (t_nodo*)malloc(sizeof(t_nodo));
  42.                 p_next = p_next -> next;
  43.                 p_next -> info = rand() % 50 + 1;
  44.             }
  45.         p_next -> next = NULL;
  46. return(p);
  47. }
  48.  
  49. void stampa_lista (t_nodo *head) {
  50.     while (head != NULL) {
  51.         printf("%d\t", head -> info);
  52.         head = head ->next;
  53.     }
  54. return;
  55. }
  56.  
  57. void trova_elemento (t_nodo *head, int numb) {
  58.     int i=1;
  59.         while (head != NULL) {
  60.             if (head -> info == numb) {
  61.                 printf("Il numero %d e' nella cella n.%d\n", numb, i);
  62.             }
  63.         i++;
  64.         head = head -> next;
  65.         }
  66.        
  67. return;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement