Advertisement
juanjo12x

Merge_Sort_Linked_List

May 20th, 2014
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.13 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  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. TNodo* Merge(TNodo* , TNodo* );
  15. void Split(TNodo *,TNodo ** ,TNodo**);
  16. void MergeSort(TNodo **);
  17.  
  18. void inserta_inicio(TNodo **ptrListaSE, int valor){
  19.     TNodo *ptrNuevo;
  20.     ptrNuevo = (TNodo*)malloc(sizeof(TNodo));
  21.     ptrNuevo->valor = valor;
  22.     ptrNuevo->ptrSig = *ptrListaSE;
  23.     *ptrListaSE = ptrNuevo;
  24. }
  25.  
  26. int lista_vacia(TNodo * ptrListaSE){
  27.     return ptrListaSE==NULL;
  28. }
  29.  
  30. void imprime_lista(TNodo * ptrListaSE){
  31.     while (ptrListaSE != NULL){
  32.         /*printf(" %d ", (*ptrListaSE).valor);*/
  33.         printf(" %d ", ptrListaSE->valor);        
  34.         ptrListaSE = ptrListaSE->ptrSig;
  35.     }
  36.     printf("NULL\n");
  37. }
  38.  
  39. void libera_lista(TNodo * ptrListaSE){    
  40.     TNodo * ptrEliminar;
  41.     while (ptrListaSE != NULL){
  42.         ptrEliminar = ptrListaSE;                
  43.         ptrListaSE = ptrListaSE->ptrSig;
  44.         free(ptrEliminar);
  45.     }    
  46. }
  47.  
  48. /* function to swap data of two nodes a and b*/
  49. void swap(TNodo *a, TNodo *b)
  50. {
  51.     int temp = a->valor;
  52.     a->valor = b->valor;
  53.     b->valor = temp;
  54. }
  55.  
  56.  
  57. /* See http://geeksforgeeks.org/?p=3622 for details of this
  58.    function */
  59. TNodo* Merge(TNodo* a, TNodo* b)
  60. {
  61.   TNodo* result = NULL;
  62.  
  63.   /* Base cases */
  64.   if (a == NULL)
  65.      return(b);
  66.   else if (b==NULL)
  67.      return(a);
  68.  
  69.   /* Pick either a or b, and recur */
  70.  // TNodo * head;
  71.   /*head=result;
  72.   while((a!=NULL) || (b!=NULL)){
  73.       if(a->valor <=b->valor ){
  74.           result->valor=a->valor;
  75.           result=result->ptrSig;
  76.          
  77.       }else{
  78.           result->valor=b->valor;
  79.           result=result->ptrSig;
  80.       }
  81.   }*/
  82.  
  83.   if (a->valor <= b->valor)
  84.   {
  85.      result = a;
  86.      result->ptrSig = Merge(a->ptrSig, b);
  87.   }
  88.   else
  89.   {
  90.      result = b;
  91.      result->ptrSig = Merge(a, b->ptrSig);
  92.   }
  93.  
  94.   /*
  95.   if (a!=NULL){
  96.       while(a!=NULL){
  97.           result->valor=a->valor;
  98.           result=result->ptrSig;
  99.           a=a->ptrSig;
  100.       }
  101.   }
  102.   if (b!=NULL){
  103.       while(b!=NULL){
  104.           result->valor=b->valor;
  105.           result=result->ptrSig;
  106.           b=b->ptrSig;
  107.       }
  108.   }*/
  109.   return(result);
  110. }
  111. /* UTILITY FUNCTIONS */
  112. /* Split the nodes of the given list into front and back halves,
  113.      and return the two lists using the reference parameters.
  114.      If the length is odd, the extra node should go in the front list.
  115.      Uses the fast/slow pointer strategy.  */
  116. void Split(TNodo* source, TNodo** frontRef, TNodo** backRef){
  117.     {
  118.   TNodo* fast;
  119.   TNodo* slow;
  120.   if (source==NULL || source->ptrSig==NULL)
  121.   {
  122.     /* length < 2 cases */
  123.     *frontRef = source;
  124.     *backRef = NULL;
  125.   }
  126.   else
  127.   {
  128.     slow = source;
  129.     fast = source->ptrSig;
  130.  
  131.     /* Advance 'fast' two nodes, and advance 'slow' one node */
  132.     while (fast != NULL)
  133.     {
  134.       fast = fast->ptrSig;
  135.       if (fast != NULL)
  136.       {
  137.         slow = slow->ptrSig;
  138.         fast = fast->ptrSig;
  139.       }
  140.     }
  141.  
  142.     /* 'slow' is before the midpoint in the list, so split it in two
  143.       at that point. */
  144.     *frontRef = source;
  145.     *backRef = slow->ptrSig;
  146.     slow->ptrSig = NULL;
  147.   }
  148. }
  149. }
  150. void Mergesort(TNodo** ptrListaSE){
  151.     //Creo un nodo que apunte a la cabeza de la lista
  152.     TNodo* head=*ptrListaSE;
  153.     TNodo *a;
  154.     TNodo*b;
  155.     /*Caso base , longitud 0 o 1*/
  156.     if ((head==NULL)|| (head->ptrSig == NULL)) return;
  157.     /*Parto head en 2 sublistas a y b*/
  158.     Split(head,&a,&b);
  159.     /*Recursivamente ordeno las sublistas*/
  160.     Mergesort(&a);
  161.     Mergesort(&b);
  162.    
  163.     /*la respuesta es unir las 2 listas ordenadas*/
  164.      * ptrListaSE= Merge(a,b);
  165.    
  166. }
  167. int main(int argc, char** argv) {
  168.     TNodo * ptrListaSE = NULL;
  169.     TNodo * ptrUltimo = NULL;
  170.     inserta_inicio(&ptrListaSE, 19);
  171.     inserta_inicio(&ptrListaSE, 15);
  172.     inserta_inicio(&ptrListaSE, 17);
  173.     inserta_inicio(&ptrListaSE, 20);
  174.     Mergesort(&ptrListaSE);
  175.     imprime_lista(ptrListaSE);
  176.     libera_lista(ptrListaSE);
  177.     return (EXIT_SUCCESS);
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement