lonsomehell

File prioritaire.

May 20th, 2013
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.07 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct ffile ffile;
  5. struct ffile
  6. {
  7.     int data;
  8.     ffile* next;
  9. };
  10. ffile* creer()
  11. {
  12.     return NULL;
  13. }
  14. int vide(ffile *f)
  15. {
  16.     if(f)
  17.         return 0;//correspond a non vide
  18.     else
  19.         return 1;//correspond a vide
  20. }
  21. void enfiler(ffile **f,int x)
  22. {
  23.     ffile* p;
  24.     p=(ffile*)malloc(sizeof(ffile));
  25.     p->data=x;
  26.     p->next=*f;
  27.     *f=p;
  28. }
  29. int defiler(ffile **f)
  30. {
  31.     if(!*f)
  32.     {
  33.         printf("Liste vide \n");
  34.         return -999;//-999 correspond a erreur.
  35.     }
  36.     else if(!(*f)->next)
  37.     {
  38.         int x=(*f)->data;
  39.         free (*f);
  40.         *f=NULL;
  41.         return x;
  42.     }
  43.     else
  44.     {
  45.         ffile *c=*f;
  46.         while(c->next->next)
  47.             c=c->next;
  48.         int x;
  49.         x=c->next->data;
  50.         free(c->next);
  51.         c->next=NULL;
  52.         return x;
  53.     }
  54. }
  55. int affiche(ffile *f)
  56. {
  57.     int i=0;
  58.     while(f){
  59.         printf("l'element %d : %d \n",i+1,f->data);
  60.         i++;
  61.         f=f->next;
  62.     }
  63.     return i;
  64. }
  65. void liberer(ffile **f)
  66. {
  67.     ffile* c=*f;
  68.     while(*f)
  69.     {
  70.         *f=(*f)->next;
  71.         free(c);
  72.         c=(*f);
  73.  
  74.     }
  75. }
  76. void create(ffile** t)
  77. {
  78.     int i;
  79.     for(i=0;i<4;i++){
  80.         t[i]=NULL;
  81.     }
  82. }
  83. void ajout(ffile **t,int n)
  84. {
  85.     int i;
  86.     printf("Donner la prioritee.\n");
  87.     do{
  88.         scanf("%d",&i);
  89.     }while((i>4)||(i<1));
  90.     enfiler(&t[i-1],n);
  91. }
  92. int deff(ffile **t)
  93. {
  94.     if(t[0])
  95.         return defiler(&t[0]);
  96.     else if(t[1])
  97.         return defiler(&t[1]);
  98.     else if(t[2])
  99.         return defiler(&t[2]);
  100.     else if(t[3])
  101.         return defiler(&t[3]);
  102.     else{
  103.         printf("Erreur\n");
  104.         return -999;//correspond a erreur.
  105.     }
  106. }
  107. vider_stream()
  108. {
  109.     char c;
  110.     while ((c = getchar()) != '\n' && c != EOF);
  111. }
  112. int main()//file a 4 priorite.
  113. {
  114.     ffile* f;
  115.     ffile** t;
  116.     t=malloc(4*sizeof(ffile*));
  117.     create(t);
  118.     ajout(t,1);
  119.     int x=affiche(t[0]);
  120.     printf("%d\n",x);
  121.     x=deff(t);
  122.     printf("%d\n",x);
  123.     printf("%p\n",t[0]);
  124.     return 0;
  125. }
  126.  
  127. /*f=creer();
  128.     int x;
  129.     char c='a';
  130.     while(c!='Q'){
  131.         printf("Donner la commande\n");
  132.         vider_stream();
  133.         scanf("%c",&c);
  134.         printf("Donner l'element a enfiler.\n");
  135.         scanf("%d",&x);
  136.         enfiler(&f,x);
  137.     }
  138.     int com;
  139.     com=affiche(f);
  140.     printf("Le nombre des elements est %d\n",com);
  141.     liberer(&f);
  142.     printf("%p\n",f);*/
Advertisement
Add Comment
Please, Sign In to add comment