Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define LUN 20
- void pushCoda(int *, int *, int *, int *);
- void popCoda(int *, int *, int *);
- void printAll(int, int, int *);
- int isFullorEmpty(int, int);
- /*main!*/
- main()
- {
- int coda[LUN];
- int puntTesta=0, puntCoda=0;
- int menu=-1, num=0;
- while(menu!=0){
- if(num==99) num=1;
- printAll(puntTesta, puntCoda, coda);
- menu=menuLista();
- switch(menu){
- case 1: {
- pushCoda(&puntTesta, &puntCoda, coda, &num); break;}
- case 2: {
- popCoda(&puntTesta, &puntCoda, coda); break;}
- case 4: {puntTesta=0; puntCoda=0; num=0;
- printf("\n\tCoda svuotata!!"); getchar(); break;}
- case 5: {menu=0; break;}
- default:{
- printf("opzione non ammessa");
- printf("\n\n\nIn attesa...\t");
- getchar();}
- }
- system("cls"); }
- return 0;
- }
- /*menu'*/
- int menuLista()
- {
- int menu;
- printf("\n------------- MENU'------------\n");
- printf("\n1. Prego prendere numerino\n\n2. Avanti il prossimo\n\n4. Svuota coda\n\n5. Exit\n\n ");
- scanf("%d", &menu);
- getchar();
- return menu;
- }
- /*inserisci elemento*/
- void pushCoda(int *t, int *c, int *coda, int *num)
- {
- int ele;
- if(isFullorEmpty(*t, (*c)+1)==-1) {printf("\nCoda piena!!\t"); getchar();}
- else {
- system("cls");
- (*num)++;
- printAll(*t, *c, coda);
- printf("\nIl tuo numero e': %d\t", *num);
- *c=*c%LUN; coda[*c]=*num; ++*c;
- getchar();}
- }
- /*togli il primo elemento in coda*/
- void popCoda(int *t, int *c, int *coda)
- {
- int size;
- system("cls");
- size=isFullorEmpty(*t, *c);
- if(size<=0) {printf("\nCoda vuota!!\t"); getchar();}
- else {
- *t=*t%LUN;
- printAll((*t)+1, *c, coda);
- printf("\nServiamo il: %d", coda[*t]);
- (*t)++;
- getchar();}
- }
- /*stampa la coda*/
- void printAll(int t, int c, int *coda)
- {
- int i, size, numPers;
- size=isFullorEmpty(t, c);
- if(size==-1 || size==0) numPers=0;
- else numPers=size;
- printf("\nPersone in coda: %d\n\t\t-------------------> Il prossimo", numPers);
- for(i=t; i!=c;){ i=i%(LUN); printf("\n\t|\t%d\t|", coda[i]); i++;}
- size=LUN-(size+1);
- for(i=0; i<size; i++) printf("\n\t|\t\t|");
- printf("\n\t\t<------------------- Ultimo arrivato\n");
- }
- /*Funzione ausiliare che controlla se la memoria è piena o vuota,
- *restituisce anche il numero di elementi presenti in coda*/
- int isFullorEmpty(int t, int c)
- {
- int size;
- if(c==0 && t==0) return 0;
- size=(c - t + LUN) % LUN;
- if(size==0) return -1;
- return size;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement