Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.01 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. typedef struct node {
  5.     int val;
  6.     struct node *next;
  7. }*List;
  8. List init_node(){
  9.     return NULL;
  10. }
  11. int est_vide(List l){
  12.     return l==NULL;
  13. }
  14. List cree_node(int val){
  15.     List new = (List) malloc(sizeof(List));
  16.     if ( new == NULL ){
  17.         printf("\n ak ghalet");
  18.         return;
  19.     }
  20.     new->next = NUll;
  21.     new->val = val;
  22.     return new;
  23. }
  24. void affiche_list(List l){
  25.     if ( l== NULL ){
  26.         printf(" la list est vide , rien d'afficher" );
  27.         return;
  28.     }
  29.     List l2=l;
  30.     while(l2!= NULL){
  31.         printf("%d",l2->val);
  32.         l2=l2->next;
  33.     }
  34. }
  35. List suprime_fin(List l){
  36.     List courant,precedent;
  37.     if(l==NULL)
  38.         return NULL;
  39.  
  40.  
  41.     if(l->next==NULL){
  42.         free(l);
  43.         return NULL;
  44.     }
  45.  
  46.  
  47.  
  48.     courant= l->next;
  49.     precedent= l;
  50.  
  51.     while(courant->next !=NULL){
  52.         precedent=precedent->next;
  53.         courant=courant->next;
  54.     }
  55.  
  56.     precedent->next=NULL;
  57.     free(courant);
  58.     return l;
  59. }
  60.  
  61. List inserer_element_fin ( List l, int e) {
  62.         List nouveau ;
  63.         List s ;
  64.         nouveau = creer_node (e) ;
  65.         if (est_vide(l))
  66.             return nouveau ;
  67.         s= liste;
  68.         while (s -> suivant != NULL)
  69.             s = s -> suivant;
  70.         s -> suivant = nouveau;
  71.         return liste;
  72. }
  73. List insire(List l, int n){
  74.     int i,v;
  75.     List l1=l;
  76.     l=init_node();
  77.     for(i=0;i<n;i++){
  78.      printf("\n give me a number: ");
  79.      scanf("%d",&v);
  80.      return inserer_element_fin (l,v);
  81. }
  82.  
  83. List invers(List l){
  84.     List p=p2=l,s;
  85.     s=init_node();
  86.     while(p2->next!=NULL){
  87.         if(est_vide(p)) return NULL;
  88.         while(p->next!=NULL)
  89.             p=p->next;
  90.         if(s == NULL)    //////////////here
  91.             s=cree_node(p->val);
  92.         else
  93.             s->next=cree_node(p->val);
  94.         p=suprime_fin(l);
  95.         p2=p2->next;
  96.     }
  97.     return s;
  98. }
  99. int main(){
  100.     List l;
  101.     int n=4;
  102.     l=insire(l,n);
  103.     l=invers(l);
  104.     affiche_list(l);
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement