Advertisement
Tankado95

Sposta elemento in testaa

May 29th, 2015
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.00 KB | None | 0 0
  1. /*Sposta l'ultima occorrenza del valore X all'inizio della lista*/
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. struct el{
  7. int info;
  8. struct el* next;
  9. };
  10. typedef struct el ElementoDiLista;
  11. typedef struct el * ListaDiElementi;
  12.  
  13. void stampa(ListaDiElementi l){
  14.  
  15.     while(l->next!=NULL)
  16.     {
  17.         printf("%d\n", l->info);
  18.         l=l->next;
  19.     }
  20. }
  21.  
  22.  
  23. void sposta (ListaDiElementi *l, int x){
  24. ListaDiElementi corr=*l, prec=NULL, save=NULL;
  25.  
  26.     if (*l!=NULL){
  27.         while(corr->next!=NULL){
  28.             if (corr->info==x){
  29.                 save=prec;
  30.                
  31.                
  32.             }
  33.             prec=corr;
  34.             corr=corr->next;
  35.         }
  36.        
  37.         prec=save->next;
  38.         save->next=save->next->next;
  39.         corr=*l;
  40.         prec->next=corr;
  41.         *l=prec;
  42.  
  43.        
  44.        
  45. }      
  46. }
  47.  
  48.  
  49.  
  50.  
  51. int main(){
  52. ListaDiElementi l=malloc(sizeof(ElementoDiLista));
  53. ListaDiElementi corr=l;
  54. int x=4,n=0;
  55. int i=0;
  56.     while(i<10)
  57.     {
  58.         scanf("%d",&n);
  59.         corr->info=n;
  60.         corr->next=malloc(sizeof(ElementoDiLista));
  61.         corr=corr->next;
  62.        
  63.         i++;
  64.     }
  65. corr->next=NULL;
  66. sposta (&l,x);
  67. stampa(l);
  68. return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement