Advertisement
Dany1858

Esercizio code con liste

Dec 9th, 2014
531
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.16 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct coda{
  5.        int inf;
  6.        struct coda *pun;
  7. };
  8.  
  9.                     /*funzioni*/
  10. int menuCoda();
  11. struct coda *creaCoda(int *);
  12. void enqueueCoda(struct coda *, int *);
  13. struct coda *dequeueCoda(struct coda *);
  14. struct coda *clearCoda(struct coda *);
  15. void printCoda(struct coda *);
  16.  
  17. main()
  18. {
  19.       struct coda *puntTesta=NULL;
  20.       int menu=-1, numerino;
  21.      
  22.       while(menu!=0){
  23.         if(menu==-1) menu=1;
  24.         else { printCoda(puntTesta); menu=menuCoda();}
  25.  
  26.         switch(menu){
  27.            case 1: {
  28.               system("cls");
  29.               if(puntTesta==NULL) puntTesta= creaCoda(&numerino);
  30.               else enqueueCoda(puntTesta, &numerino); break;}
  31.            case 2: {
  32.               puntTesta= dequeueCoda(puntTesta); break;}
  33.            case 3: {puntTesta= clearCoda(puntTesta); break;}
  34.            case 4: {printCoda(puntTesta); printf("\n\n\nIn attesa..."); getchar(); break;}
  35.            case 5: {menu=0; break;}
  36.            default:{
  37.               printf("opzione non ammessa");
  38.               printf("\n\n\nIn attesa...\t");
  39.               getchar();}
  40.            }  
  41.       system("cls"); }
  42.       return 0;
  43. }
  44.  
  45.  
  46.                    /*menu'*/
  47. int menuCoda()
  48. {
  49.        int menu;
  50.        printf("\n\n------------- MENU'------------\n");
  51.        printf("\n1. Prendi numerino\n\n2. Avanti il prossimo\n\n3. Svuota pila\n\n5. Exit\n\n ");
  52.        scanf("%d", &menu);
  53.        getchar();
  54.        return menu;
  55. }
  56.  
  57.                      /*crea lista*/
  58. struct coda *creaCoda(int *num)
  59. {    
  60.        struct coda *p;
  61.        p=(struct coda *)malloc(sizeof(struct coda));
  62.        printf("\nIl suo numero e': 1\t");
  63.        getchar();
  64.        *num=1;
  65.        p->inf=1;
  66.        p->pun=NULL;
  67.        return(p);
  68. }
  69.  
  70. void enqueueCoda(struct coda *p, int *num)
  71. {      
  72.        system("cls");
  73.        printCoda(p);
  74.        struct coda *paus=p;
  75.        while(p!=NULL){
  76.            paus=p;
  77.             p=p->pun;}
  78.        ++*num; if(*num==100) *num=1;
  79.        printf("\n\nIl suo numero e': %d\t", *num);
  80.        getchar();
  81.        paus->pun=(struct coda *)malloc(sizeof(struct coda));
  82.        paus=paus->pun;
  83.        paus->inf=*num; paus->pun=NULL;
  84. }
  85.  
  86. struct coda *dequeueCoda(struct coda *p)
  87. {
  88.        struct coda *paus=p;
  89.        system("cls");
  90.        printCoda(p);
  91.        p=p->pun;
  92.        printf("\n\n\tServiamo il: %d", paus->inf);
  93.        printf("\n\nIn attesa...");
  94.        getchar();
  95.        free(paus);
  96.        return p;
  97. }
  98.  
  99. struct coda *clearCoda(struct coda *p)
  100. {
  101.        struct coda *paus;
  102.        system("cls");
  103.        while(p!=NULL){
  104.            paus=p;
  105.            p=p->pun;
  106.            free(paus);}
  107.        printf("\n\n\n\n\t\tCoda svuotata!!\n\n\n\n");
  108.        getchar();
  109.        return NULL;
  110. }
  111.            
  112. void printCoda(struct coda *p)
  113. {
  114.      int numPers=0;
  115.      system("cls");
  116.      printf("\n\t\t-------------------> Il prossimo");
  117.      printf("\n\t|\t \t|");
  118.      while(p!=NULL){
  119.            printf("\n\t|\t%d\t|", p->inf);
  120.            numPers++;
  121.            p=p->pun;}
  122.      printf("\n\t|\t \t|");
  123.      printf("\n\t\t<------------------- Ultimo arrivato\n\nPersone in coda: %d\t", numPers);
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement