Advertisement
matteopunk1

Rubrica

Apr 30th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.90 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4.  
  5. typedef struct Nodo{    //typedef serve a dichiarare un nuovo tipo di variabile, in questo caso la variabile è struct nodo.
  6.     char nome[20];
  7.     char cognome[30];
  8.     int anno;            //Valore contenuto nella strutture, è il valore che andrò a modificare nelle funzioni e che si vede quando stampo la lista.
  9.     Nodo *next;         //è una variabile di tipo nodo che punta alla struttura successiva.
  10. };
  11. Nodo *inserimentoUtenteOrdinato(Nodo *, char[], char[], int);
  12. void ricercaUtente(Nodo *, char[]);
  13. void visualizzaLista(Nodo *);
  14.  
  15. int main()
  16. {
  17.     Nodo *testa;
  18.     testa=0;
  19.     int scelta,anno;
  20.     char strNome[20];
  21.     char strCognome[30];
  22.     char ricercaNome[20];
  23.  
  24.     do{
  25.         system("CLS");
  26.         printf("1 -> Inserire utente.\n");
  27.         printf("2 -> Ricerca utente.\n");
  28.         printf("3 -> Visualizza rubrica.\n");
  29.  
  30.         printf("Scelta: ");
  31.         scanf("%d",&scelta);
  32.         fflush(stdin);
  33.  
  34.         switch(scelta)
  35.         {
  36.             case 1:{
  37.                 printf("Inserisci nome: ");
  38.                 scanf("%s",&strNome);
  39.                 fflush(stdin);
  40.  
  41.                 printf("Inserisci cognome: ");
  42.                 scanf("%s",&strCognome);
  43.                 fflush(stdin);
  44.  
  45.                 printf("Inserisci anno: ");
  46.                 scanf("%d",&anno);
  47.                 fflush(stdin);
  48.  
  49.                 testa=inserimentoUtenteOrdinato(testa,strNome,strCognome,anno);
  50.                 break;
  51.             }
  52.             case 2:{
  53.                 printf("Inserisci il nome da cercare: ");
  54.                 scanf("%s",&ricercaNome);
  55.                 fflush(stdin);
  56.  
  57.                 ricercaUtente(testa,ricercaNome);
  58.                 break;
  59.             }
  60.             case 3:{
  61.                 visualizzaLista(testa);
  62.                 break;
  63.             }
  64.         }
  65.         getchar();
  66.     }while(scelta!=0);
  67.  
  68.     return 0;
  69. }
  70. //Case 1
  71. Nodo *inserimentoUtenteOrdinato(Nodo *testa, char nome[20], char cognome[30], int _anno)
  72. {
  73.     if(testa==0) //se la lista è vuota.
  74.     {
  75.         testa=(Nodo *)malloc(sizeof(Nodo)); //alloca una nuova struttura, creo lo spazio per la struttura. malloc crea un nuovo spazio della grandezza di Nodo.
  76.         strcpy(testa->nome,nome);
  77.         strcpy(testa->cognome,cognome);
  78.         testa->anno=_anno;
  79.         testa->next=0;
  80.     }
  81.     else        //altrimenti quando ho già dei nodi nella lista
  82.     {
  83.         /*Nodo *tmp, *tmp2;
  84.         char box[20];
  85.  
  86.         tmp=testa;
  87.         testa=(Nodo *)malloc(sizeof(Nodo));
  88.         strcpy(testa->nome,nome);
  89.         strcpy(testa->cognome,cognome);
  90.         testa->anno=_anno;
  91.         testa->next=tmp;
  92.         while(tmp->next!=0){
  93.             tmp2=tmp;
  94.             for(int i=0;i<strlen(tmp->nome);i++){
  95.                 if(strcmp(tmp->nome,tmp2->nome)!=0){
  96.                     strcpy(box,tmp->nome);
  97.                     strcpy(tmp->nome,tmp2->nome);
  98.                     strcpy(tmp2->nome,box);
  99.                 }
  100.             }
  101.             tmp=tmp->next;
  102.         }*/
  103.         Nodo *nuova, *previous, *current;
  104.  
  105.         nuova = (Nodo *)malloc(sizeof(Nodo));
  106.  
  107.         strcpy(nuova->nome,nome);
  108.         strcpy(nuova->cognome,cognome);
  109.         nuova->anno=_anno;
  110.  
  111.         /*if (nuova->nome == NULL) {
  112.               printf ("Memoria non disponibile\n");
  113.               exit(1);
  114.          }*/
  115.  
  116.         previous = NULL;
  117.         current = testa;
  118.  
  119.         while(current != NULL && strcmp(nome, current->nome) > 0){
  120.             previous = current;
  121.             current = current->next;
  122.         }
  123.  
  124.         if(previous == NULL){
  125.             nuova->next = testa;
  126.             testa = nuova;
  127.         }
  128.         else{
  129.             previous->next = nuova;
  130.             nuova->next = current;
  131.         }
  132.     }
  133.     return testa;
  134. }
  135. //Case 2
  136. void ricercaUtente(Nodo *testa, char nomeDaCercare[20])
  137. {
  138.     if(testa==0){
  139.         printf("Lista vuota!");
  140.         getchar();
  141.     }
  142.     else{
  143.         Nodo *tmp;
  144.         tmp=testa;
  145.         while(tmp!=0){
  146.                 if(strcmp(tmp->nome,nomeDaCercare)==0){
  147.                     printf("%s %s %d\n",tmp->nome,tmp->cognome,tmp->anno);
  148.                 }
  149.             tmp=tmp->next;
  150.         }
  151.     }
  152. }
  153. //Case 3
  154. void visualizzaLista(Nodo *testa)
  155. {
  156.     if(testa==0)
  157.     {
  158.         printf("Lista Vuota!\n");
  159.         getchar();
  160.     }
  161.     else
  162.     {
  163.         Nodo *tmp;
  164.         printf("\n\nTesta: %d\n\n",testa);      //Stampo il valore di testa.
  165.         tmp=testa;      //metto tmp uguale a testa
  166.         while(tmp!=0)       //giro finchè tmp è diverso da zero. Quando tmp è 0 esco dal ciclo. Nel while si incrementa dentro il ciclo (cnt++) e nel for direttamente nella prima riga.
  167.         {
  168.             printf("%d %s %s %d %d \n",tmp,tmp->nome, tmp->cognome, tmp->anno,tmp->next);   //stampo il valore di tmp, il valore di val e il valore di next.
  169.             tmp=tmp->next;          //metto tmp uguale a tmp->next per passare al nodo successivo.
  170.         }
  171.     }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement