Advertisement
Guest User

supprimerElement

a guest
Oct 31st, 2014
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.05 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct noeud //structure liste chaînée
  5. {
  6.   int valeur; // contenu de la valeur du noeud (ex 50,70,...)
  7.   struct noeud *next; // case du prochain element (=NULL si fin de la liste sinon c
  8. };
  9.  
  10. void afficher(struct noeud *pointeurTeteListe);
  11.  
  12. struct noeud *ajouterElementDebut(struct noeud *pointeurTmp, int nouvelleValeur);
  13. void ajouterElementFin(struct noeud *pointeurTmp, int nouvelleValeur);
  14.  
  15. struct noeud *supprimerElement(struct noeud *pointeurTmp, int nouvelleValeur);
  16.  
  17.  
  18.  
  19. int main()
  20. {
  21.   struct noeud *pointeurTeteListe=NULL;
  22.   struct noeud *pointeurNouvelElement;
  23.  
  24.   //nouvel élément de valeur 70
  25.   pointeurNouvelElement=malloc(sizeof(struct noeud));
  26.   pointeurNouvelElement->valeur=70;
  27.   pointeurNouvelElement->next=pointeurTeteListe; // nan c'est bon réesaye
  28.   pointeurTeteListe=pointeurNouvelElement;
  29.  
  30.   //nouvel élément de valeur 50
  31.   pointeurNouvelElement=malloc(sizeof(struct noeud));
  32.   pointeurNouvelElement->valeur=50;
  33.   pointeurNouvelElement->next=pointeurTeteListe;
  34.   pointeurTeteListe=pointeurNouvelElement;
  35.  
  36.   afficher (pointeurTeteListe);
  37.  
  38.   pointeurTeteListe = ajouterElementDebut(pointeurNouvelElement, 5);
  39.   afficher(pointeurTeteListe);
  40.  
  41.   ajouterElementFin(pointeurNouvelElement, 1911);
  42.   afficher(pointeurTeteListe);
  43.  
  44.   supprimerElement(pointeurTeteListe, 5);
  45.   afficher(pointeurTeteListe);
  46.  
  47.   return 0;
  48. }
  49.  
  50.  
  51. void afficher(struct noeud *pointeurTeteListe)//fonction d'affichage des listes chaînée
  52. {
  53.     struct noeud *pointeurLecture=pointeurTeteListe; // <= ce pointeur la
  54.  
  55.     while(pointeurLecture!=NULL)
  56.     {
  57.         printf("%d", pointeurLecture->valeur);
  58.  
  59.         if(pointeurLecture->next!=NULL)
  60.         {
  61.             printf(";");
  62.         }
  63.         else
  64.         {
  65.             printf("\n");
  66.         }
  67.       pointeurLecture=pointeurLecture->next;
  68.     }
  69. }
  70.  
  71. struct noeud *ajouterElementDebut(struct noeud *pointeurTmp, int nouvelleValeur)//Ajout d'un élément en le placant au début
  72. {
  73.         struct noeud *pointeurNouvelElement=malloc(sizeof(struct noeud));
  74.         pointeurNouvelElement->valeur=nouvelleValeur;
  75.         pointeurNouvelElement->next=pointeurTmp;
  76.         pointeurTmp=pointeurNouvelElement;
  77.  
  78.         return pointeurTmp;
  79. }
  80.  
  81. void ajouterElementFin(struct noeud *pointeurTmp, int nouvelleValeur)
  82. {
  83.  
  84.         struct noeud *pointeurNouvelElement=malloc(sizeof(struct noeud));
  85.         pointeurNouvelElement->valeur=nouvelleValeur;
  86.         pointeurNouvelElement->next=NULL;
  87.  
  88.         struct noeud *pointeurTeteListe=pointeurTmp;
  89.         while(pointeurTeteListe->next != NULL)
  90.         {
  91.                 pointeurTeteListe=pointeurTeteListe->next;
  92.         }
  93.         pointeurTeteListe->next = pointeurNouvelElement;
  94.  
  95. }
  96.  
  97. struct noeud *supprimerElement(struct noeud *pointeurTmp, int nouvelleValeur)
  98. {
  99.     struct noeud *pLecture = pointeurTmp;
  100.     struct noeud *pTrouve = NULL;
  101.     while (pLecture != NULL)
  102.     {
  103.         if(pLecture->valeur == nouvelleValeur)
  104.         {
  105.             pTrouve = pLecture;
  106.         }
  107.         pLecture = pLecture->next;
  108.     }
  109.     if (pTrouve != NULL)
  110.     {
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement