Advertisement
Guest User

ESAME0906ELE

a guest
Mar 21st, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.97 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define DIM 50
  4.  
  5. struct nodo {
  6. char nome [DIM];
  7. char cognome [DIM];
  8. int matricola;
  9. struct nodo *next;
  10. };
  11.  
  12.  
  13. struct nodo *aggiungi (struct nodo *testa) {
  14. int numerostudenti;
  15. int i;
  16. printf("Quanti studenti vuoi inserire?\n");
  17. scanf("%d", &numerostudenti);
  18.  
  19. if (numerostudenti==NULL){
  20.     printf("La lista e' vuota, non abbiamo aggiunto studenti!\n");
  21. }
  22. else{
  23.     testa=malloc(sizeof(struct nodo));
  24.     printf("inserisci nome del primo studente\n");
  25.     fgets(testa->nome,DIM,stdin);
  26.     printf("inserisci cognome del primo studente\n");
  27.     fgets(testa->cognome, DIM, stdin);
  28.     printf("inserisci matricola del primo studente\n");
  29.     scanf("%d",&testa->matricola);
  30.  
  31.     struct nodo *p=NULL;
  32.     p=testa;
  33.     for (i=2;i<=numerostudenti;i++){
  34.     while(p!=NULL) {
  35.             p=malloc(sizeof(struct nodo));
  36.             printf("inserisci nome e cognome dello studente\n");
  37.             fgets(p->nome, DIM, stdin);
  38.             fgets(p->cognome, DIM, stdin);
  39.             printf("inserisci matricola dello studente\n");
  40.             scanf("%d",&p->matricola);
  41.             p=p->next;
  42.     }
  43.     p->next=NULL;
  44.     }
  45.  
  46. }
  47. return testa;
  48.  
  49. };
  50.  
  51. void visualizzalista (struct nodo *testa) {
  52. struct nodo *p=NULL;
  53.  
  54. if(testa==NULL){
  55.     printf("La lista e' vuota!");
  56.     }
  57. else{
  58.         p=testa;
  59.         while (p!=NULL) {
  60.                 printf("Visualizza nome e cognome dello studente %s %s\n", p->nome, p->cognome);
  61.                 fgets(p->nome, DIM, stdin);
  62.                 fgets(p->cognome, DIM, stdin);
  63.                 printf("Visualizza matricola dello studente %d\n", p->matricola);
  64.                 scanf("%d", &p->matricola);
  65.                 }
  66. if (p==NULL) {
  67.     printf("siamo arrivati alla fine della lista\n");
  68. }
  69. }
  70. }
  71.  
  72.  
  73. struct nodo *elimina (struct nodo *testa) {
  74. int matricolacercata;
  75. if (testa==NULL) {
  76.     printf("La lista e' vuota!\n");
  77.     }
  78. else    {
  79.     struct nodo *p=NULL;
  80.     struct nodo *prec=NULL;
  81.     p=testa;
  82.  
  83.         while (p!=NULL && p->matricola!=matricolacercata) {
  84.         p=p->next;
  85.         }
  86.     if (p=NULL) {
  87.         printf("il valore cercato non e' presente nella lista\n");
  88.     }
  89.     else {
  90.         printf("Ho trovato il valore da eliminare\n");
  91.         prec->next=p->next;
  92.         free(p);
  93.     }
  94.  
  95. }
  96. return testa;
  97. };
  98.  
  99.  
  100. int main()
  101. {
  102.     struct nodo *testa=NULL;
  103.     int scelta=-1;
  104.     while(scelta!=0) {
  105.         printf("MENU\n0. esci\n1.inseriscistudente\n2.visualizzalista\n3.eliminastudente\nSCELTA:\n");
  106.         scanf("%d",&scelta);
  107.         switch (scelta) {
  108.                 case 0: printf("ESCI dal programma!\n");
  109.                     break;
  110.                 case 1: testa= aggiungi(testa);
  111.                     break;
  112.                 case 2: visualizzalista(testa);
  113.                     break;
  114.                 case 3: testa=elimina(testa);
  115.                     break;
  116.                 default: printf("scelta non corretta!\n");
  117.  
  118.                 }
  119.     }
  120.     return 0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement