Advertisement
juanjo12x

Selection_Sort_Linked_List

May 21st, 2014
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.39 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <limits.h>
  4. typedef struct nodo{
  5.   int valor;
  6.   struct nodo* ptrSig;
  7. }TNodo;
  8.  
  9. int lista_vacia(TNodo *);
  10. void inserta_inicio(TNodo **, int);
  11.  
  12. void imprime_lista(TNodo *);
  13. void libera_lista(TNodo *);
  14. void Selection_Sort(TNodo *);
  15.  
  16. void inserta_inicio(TNodo **ptrListaSE, int valor){
  17.     TNodo *ptrNuevo;
  18.     ptrNuevo = (TNodo*)malloc(sizeof(TNodo));
  19.     ptrNuevo->valor = valor;
  20.     ptrNuevo->ptrSig = *ptrListaSE;
  21.     *ptrListaSE = ptrNuevo;
  22. }
  23.  
  24. int lista_vacia(TNodo * ptrListaSE){
  25.     return ptrListaSE==NULL;
  26. }
  27.  
  28. void imprime_lista(TNodo * ptrListaSE){
  29.     while (ptrListaSE != NULL){
  30.         /*printf(" %d ", (*ptrListaSE).valor);*/
  31.         printf(" %d ", ptrListaSE->valor);        
  32.         ptrListaSE = ptrListaSE->ptrSig;
  33.     }
  34.     printf("NULL\n");
  35. }
  36.  
  37. void libera_lista(TNodo * ptrListaSE){    
  38.     TNodo * ptrEliminar;
  39.     while (ptrListaSE != NULL){
  40.         ptrEliminar = ptrListaSE;                
  41.         ptrListaSE = ptrListaSE->ptrSig;
  42.         free(ptrEliminar);
  43.     }    
  44. }
  45. /* function to swap data of two nodes a and b*/
  46. void swap(TNodo *a, TNodo *b)
  47. {
  48.     int temp = a->valor;
  49.     a->valor = b->valor;
  50.     b->valor = temp;
  51. }
  52. void Selection_Sort(TNodo* ptrListaSE){
  53.     int min;
  54.     TNodo* ptrAnterior;TNodo* ptrRecorrido;
  55.     TNodo* ptrMin;
  56.     ptrAnterior=ptrListaSE;
  57.     ptrRecorrido=ptrListaSE;
  58.     if (ptrAnterior== NULL) printf("Empty Linked List");
  59.     while (ptrAnterior!=NULL){
  60.         ptrMin=NULL;
  61.         ptrRecorrido=ptrAnterior;
  62.         min=INT_MAX;
  63.         while(ptrRecorrido!=NULL){
  64.             if(ptrRecorrido->valor <  min){
  65.                 min=ptrRecorrido->valor;
  66.                 ptrMin=ptrRecorrido;
  67.             }
  68.             ptrRecorrido=ptrRecorrido->ptrSig;
  69.         }
  70.        
  71.         if (ptrMin!=NULL) swap(ptrAnterior,ptrMin);
  72.         ptrAnterior=ptrAnterior->ptrSig;
  73.     }
  74.    
  75.    
  76. }
  77.  
  78. int main(int argc, char** argv) {
  79.     TNodo * ptrListaSE = NULL;
  80.     TNodo * ptrUltimo = NULL;
  81.     inserta_inicio(&ptrListaSE, 19);
  82.     inserta_inicio(&ptrListaSE, 15);
  83.     inserta_inicio(&ptrListaSE, 17);
  84.     inserta_inicio(&ptrListaSE, 37);
  85.     inserta_inicio(&ptrListaSE, 3);
  86.     inserta_inicio(&ptrListaSE, 1);
  87.     inserta_inicio(&ptrListaSE, 20);
  88.    
  89.     Selection_Sort(ptrListaSE);
  90.     imprime_lista(ptrListaSE);
  91.     libera_lista(ptrListaSE);
  92.     return (EXIT_SUCCESS);
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement