lonsomehell

liste

Mar 14th, 2013
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.20 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef struct element element;
  4. struct element
  5. {
  6.     int data;
  7.     element* next;
  8. };
  9. typedef element* liste;
  10. liste creer()
  11. {
  12.     return NULL;
  13. }
  14. liste adjq(liste ancienne,int el)
  15. {
  16.     liste l,p;
  17.     l=ancienne;
  18.     p=(liste)malloc(sizeof(element));
  19.     p->data=el;
  20.     p->next=NULL;
  21.     if(l==NULL)
  22.         return p;
  23.     else{
  24.         for(;l->next;l=l->next);
  25.         l->next=p;
  26.     }
  27.     return l;
  28. }
  29. liste adjt(liste ancienne,int el)
  30. {
  31.     liste p;
  32.     p=(liste)malloc(sizeof(element));
  33.     p->data=el;
  34.     p->next=ancienne;
  35.     return p;
  36. }
  37. liste concat(liste ancienne1,liste ancienne2)
  38. {
  39.     liste l;
  40.     l=ancienne1;
  41.     if(l==NULL)
  42.         return ancienne2;
  43.     else{
  44.         while(l->next)
  45.             l=l->next;
  46.         l->next=ancienne2;
  47.     }
  48.     return ancienne1;
  49. }
  50. liste sup_t(liste ancienne)
  51. {
  52.     liste l;
  53.     l=ancienne;
  54.     if(l!=NULL){
  55.         l=l->next;
  56.         free(ancienne);
  57.     }
  58.     return l;
  59. }
  60. liste sup_d(liste ancienne)
  61. {
  62.     liste l;
  63.     l=ancienne;
  64.     if(l->next==NULL){
  65.         ancienne=NULL;
  66.         free(l);
  67.         return ancienne;
  68.     }
  69.        
  70.     else{
  71.         if(l!=NULL){
  72.         while(l->next->next)
  73.             l=l->next;
  74.         free(l->next);
  75.         l->next=NULL;
  76.         return ancienne;
  77.         }
  78.     }
  79.     return ancienne;//case ancienne==NULL :).
  80. }
  81. liste redondance(liste l)
  82. {
  83.     liste c,p;
  84.     if(!l)
  85.         return l;
  86.     /*c->data=l->data;
  87.     c->next=l->next;*/
  88.     c=l;
  89.     while(c){
  90.         while((c->next)&&(c->data==c->next->data)){
  91.             p=c->next;
  92.             c->next=c->next->next;
  93.             free(p);
  94.         }
  95.         c=c->next;
  96.     }
  97.     return l;
  98. }
  99. int affiche(liste l)//pour etre homogene.Not really needed.
  100. {
  101.     int i=0;
  102.     liste c;
  103.     c=l;
  104.     while(c){
  105.         printf("L'entier %d : %d\n",i+1,c->data);
  106.         i++;
  107.         c=c->next;
  108.     }
  109.     return i;
  110. }
  111. int main()
  112. {
  113.     liste l,p;
  114.     l=creer();
  115.     int el;
  116.     el=1;
  117.     /*l=adjq(l,el);
  118.     l=adjt(l,0);
  119.     p=creer();
  120.     p=malloc(sizeof(element));
  121.     p->data=l->data;
  122.     p->next=NULL;
  123.     printf("%d %d\n",l->data,l->next->data);
  124.     printf("%d\n",l->next->data);
  125.     l=concat(l,p);
  126.     printf("%d %d %d\n",l->data,l->next->data,l->next->next->data);
  127.     p=sup_d(l);
  128.     printf("%d\n",l->data);*/
  129.     l=adjq(l,2);
  130.     l=adjq(l,2);
  131.     l=adjq(l,2);
  132.     //l=adjt(l,0);
  133.     l=adjq(l,3);
  134.     l=adjq(l,3);
  135.     l=adjq(l,3);
  136.     el=affiche(l);
  137.     printf("%d\n",el);
  138.     l=redondance(l);
  139.     el=affiche(l);
  140.     printf("%d\n",el);
  141.     return 0;
  142. }
  143. //working damn it.At last.
Advertisement
Add Comment
Please, Sign In to add comment