Advertisement
juan_de99

Untitled

Nov 5th, 2021
820
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.57 KB | None | 0 0
  1. void crearListaD(tListaD *lista)
  2. {
  3.     *lista=NULL;
  4. }
  5. int insertarOrdenadoD(tListaD *lista,const void *dato,unsigned tam,int(*cmp)(const void *d1,const void *d2))
  6. {
  7.     tNodo *nue,
  8.           *act=*lista,
  9.            *auxAnt,
  10.            *auxSig;
  11.  
  12.     if(!(nue=malloc(sizeof(tNodo))))
  13.         return -1;
  14.     if(!(nue->dato=malloc(tam)))
  15.     {
  16.         free(nue);
  17.         return -1;
  18.     }
  19.  
  20.     memcpy(nue->dato,dato,tam);
  21.     nue->tam=tam;
  22.  
  23.     if(!(*lista))
  24.     {
  25.         auxSig=NULL;
  26.         auxAnt=NULL;
  27.     }
  28.  
  29.     else
  30.     {
  31.         while(act->ant && cmp(dato,act->dato)<0)
  32.             act=act->ant;
  33.  
  34.  
  35.         while(act->sig && cmp(dato,act->dato)>=0)
  36.             act=act->sig;
  37.  
  38.         if(cmp(dato,act->dato) > 0)
  39.         {
  40.             auxSig=act->sig;
  41.             auxAnt=act;
  42.         }
  43.         else
  44.         {
  45.             auxSig=act;
  46.             auxAnt=act->ant;
  47.         }
  48.     }
  49.  
  50.     nue->sig=auxSig;
  51.     nue->ant=auxAnt;
  52.  
  53.     if(auxAnt)
  54.         auxAnt->sig=nue;
  55.     if(auxSig)
  56.         auxSig->ant=nue;
  57.  
  58.    *lista=nue;
  59.  
  60.     return 1;
  61.  
  62. }
  63.  
  64. void recorrerListaD(tListaD *lista,void(*accion)(void *dato))
  65. {
  66.     tNodo *act=*lista,
  67.            *auxSig,
  68.            *auxAnt;
  69.  
  70.     if(!(*lista))
  71.         return;
  72.  
  73.     auxSig=act->sig;
  74.     auxAnt=act->ant;
  75.     while(auxAnt)
  76.     {
  77.         act=auxAnt;
  78.         auxAnt=act->ant;
  79.         auxSig=act->sig;
  80.     }
  81.  
  82.     while(act)
  83.     {
  84.         accion(act->dato);
  85.         act=auxSig;
  86.         if(act)
  87.         {
  88.             auxSig=act->sig;
  89.             auxAnt=act->ant;
  90.         }
  91.     }
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement