Biddle

Q2.C (test) supprimer_majeur

Mar 28th, 2014
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct student {
  4.   char nom[25];
  5.   int age;
  6. };
  7. struct list_student {
  8.   struct student Letudiant;
  9.   struct list_student * suivant;
  10. };
  11. struct list_student * ajout_etudiant (struct student S, struct list_student *L) {
  12.   struct list_student * res = NULL;
  13.   res = (struct list_student *)malloc(sizeof(struct list_student));
  14.   res->Letudiant = S;
  15.   res->suivant = L;
  16.   return res;
  17. }
  18. void afficher_etudiant(struct student E) {
  19.   printf("%s\n", E.nom);
  20.   printf("%d\n",E.age);
  21. }
  22. void afficher_element(struct list_student *el) {
  23.   afficher_etudiant(el->Letudiant);
  24. }
  25. void afficher_list(struct list_student *el) {
  26.   while(el!=NULL)
  27.   {
  28.     afficher_element(el);
  29.     el = el->suivant;
  30.   }  
  31. }
  32. struct list_student * creer_liste ()
  33. {
  34.         struct list_student * resultat = NULL;
  35.  
  36.         struct student s1 = {"Isabelle", 19};
  37.         struct student s2 = {"Daniel", 23};
  38.         struct student s3 = {"Alexandre", 25};
  39.         struct student s4 = {"Marie", 17};
  40.         struct student s5 = {"Bruno", 27};
  41.         struct student s6 = {"Antoine", 15};
  42.         struct student s7 = {"Jean", 22};
  43.         struct student s8 = {"Clement", 23};
  44.         struct student s9 = {"Diego", 24};
  45.         struct student s0 = {"Caroline", 16};
  46.         resultat = ajout_etudiant (s1, resultat);
  47.         resultat = ajout_etudiant (s2, resultat);
  48.         resultat = ajout_etudiant (s3, resultat);
  49.         resultat = ajout_etudiant (s4, resultat);
  50.         resultat = ajout_etudiant (s5, resultat);
  51.         resultat = ajout_etudiant (s6, resultat);
  52.         resultat = ajout_etudiant (s7, resultat);
  53.         resultat = ajout_etudiant (s8, resultat);
  54.         resultat = ajout_etudiant (s9, resultat);
  55.         resultat = ajout_etudiant (s0, resultat);
  56.  
  57.         return resultat;
  58. }
  59. struct list_student * supprimer_elements (int N1, int N2, struct list_student * L)
  60. {
  61.   int lisLong = 0;
  62.   struct list_student * Ltemp = L;
  63.   while(Ltemp!=NULL)
  64.   {
  65.     Ltemp = Ltemp->suivant;
  66.     lisLong++;
  67.   }
  68.   lisLong = lisLong - 1;
  69.   if(L!=NULL && N2>=N1 && N2<=lisLong)
  70.   {
  71.     int i = 0;
  72.     struct list_student * Ldebut;
  73.     Ldebut=(struct list_student*)malloc(sizeof(struct list_student));
  74.     Ldebut = L;
  75.     while(i<N1-1 && N1!=0)
  76.     {
  77.       Ldebut = Ldebut->suivant;
  78.       i++;
  79.     }
  80.     struct list_student * Lfin;
  81.     Lfin=(struct list_student*)malloc(sizeof(struct list_student));
  82.     Lfin = L;
  83.     i=0;
  84.     while(i<N2)
  85.     {
  86.       Lfin = Lfin->suivant;
  87.       i++;
  88.     }
  89.     Ldebut->suivant = Lfin->suivant;
  90.     if(N1 == 0)
  91.     {
  92.       L = Lfin;
  93.     }
  94.   }
  95.   return L;
  96. }
  97. int main() {
  98.   struct list_student * ls =  NULL;
  99.   ls = creer_liste ();
  100.   printf("Ancienne: \n");
  101.   afficher_list(ls);
  102.   ls = supprimer_elements(2, 5, ls);
  103.   printf("\nNouvelle:\n");
  104.   afficher_list(ls);
  105.  
  106.   return 1;
  107. }
Add Comment
Please, Sign In to add comment