Advertisement
Guest User

Split Linked List for Natural Merge Sort

a guest
Mar 23rd, 2019
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.01 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<time.h>
  4.  
  5.  
  6. struct TNode
  7. {
  8.      int data;
  9.      struct TNode *next;
  10.      struct TNode *prev;  
  11. };
  12.  
  13. struct TList
  14. {
  15.     struct TNode *head;
  16.     struct TNode *tail;  
  17. };
  18.  
  19. void ListInit(struct TList *L)
  20. {
  21.      (*L).head = NULL;
  22.      (*L).tail = NULL;
  23. }
  24.  
  25. int ListIsEmpty(struct TList L)
  26. {
  27.    return (L.head == NULL);  
  28. }
  29.  
  30. void ListDisplayForward(struct TList L)
  31. {
  32.      int counter = 0;
  33.      struct TNode *curr = L.head;
  34.      printf("List (head-->tail): ");
  35.      while(curr != NULL)
  36.      {
  37.          printf("%d -> ",curr->data);      
  38.          curr = curr->next;
  39.          counter++;      
  40.      }
  41.      printf("NULL \n");
  42.      printf("Liczba wezlow listy : %d \n",counter);
  43. }
  44.  
  45. void ListDisplayBackward(struct TList L)
  46. {
  47.      int counter = 0;
  48.      struct TNode *curr = L.tail;
  49.      printf("List (tail-->head): ");
  50.      while(curr != NULL)
  51.      {
  52.          printf("%d -> ",curr->data);      
  53.          curr = curr->prev;
  54.          counter++;      
  55.      }
  56.      printf("NULL \n");
  57.      printf("Liczba wezlow listy : %d \n",counter);
  58. }
  59.  
  60.  
  61. void ListInsert(struct TList *L,int k)
  62. {
  63.     struct TNode *x = (struct TNode *)malloc(sizeof(struct TNode )) ;
  64.     x->data = k;
  65.     x->next = (*L).head;
  66.     if((*L).head != NULL)
  67.        (*L).head->prev = x;
  68.     (*L).head = x;
  69.     x->prev = NULL;
  70.     if(x->next == NULL)
  71.         (*L).tail = x;      
  72. }
  73.  
  74. void ListDelete(struct TList *L)
  75. {
  76.      struct TNode *x = (*L).head;
  77.      if((*L).head != NULL)
  78.      {
  79.         if((*L).head->next == NULL)    
  80.              (*L).tail = (*L).head->prev;              
  81.          (*L).head = x->next;
  82.          if(x->next != NULL)
  83.             x->next->prev = x->prev;      
  84.          free(x);    
  85.      }
  86. }
  87.  
  88. int ListSplit(struct TList L,struct TList *A,struct TList *B)
  89. {
  90.     int isEmptyB,swapLists;
  91.     struct TNode *currNode,*prevNode;
  92.     swapLists = 1;
  93.     isEmptyB = 1;
  94.     ListInit(A);
  95.     ListInit(B);
  96.     if(L.head != NULL)
  97.     {        
  98.         // Usun wezel z glowy listy L
  99.         //Wstaw wezel za ogon listy A
  100.         currNode = L.head;
  101.         if(L.head->next == NULL)
  102.            L.tail = NULL;
  103.         else
  104.            L.head->next->prev = NULL;
  105.         L.head = L.head->next;
  106.        
  107.         if((*A).head == NULL)
  108.            (*A).head = currNode;
  109.         else
  110.         {
  111.             (*A).tail->next = currNode;
  112.             currNode->prev = (*A).tail;
  113.         }      
  114.          (*A).tail = currNode;
  115.          currNode->next = NULL;
  116.                      
  117.         prevNode = currNode;
  118.     }
  119.     while(L.head != NULL)
  120.     {
  121.         // Usun wezel z glowy listy L
  122.         currNode = L.head;
  123.         if(L.head->next == NULL)
  124.            L.tail = NULL;
  125.         else
  126.            L.head->next->prev = NULL;
  127.         L.head = L.head->next;
  128.        
  129.         if(prevNode->data > currNode->data)
  130.            swapLists = !swapLists;
  131.         if(swapLists)
  132.         {
  133.            //Wstaw wezel za ogon listy A
  134.            if((*A).head == NULL)
  135.            (*A).head = currNode;
  136.             else
  137.             {
  138.                 (*A).tail->next = currNode;
  139.                 currNode->prev = (*A).tail;
  140.             }      
  141.              (*A).tail = currNode;
  142.              currNode->next = NULL;
  143.         }
  144.         else
  145.         {
  146.             //Wstaw wezel za ogon listy B
  147.             if((*B).head == NULL)
  148.            (*B).head = currNode;
  149.             else
  150.             {
  151.                 (*B).tail->next = currNode;
  152.                 currNode->prev = (*B).tail;
  153.             }      
  154.              (*B).tail = currNode;
  155.              currNode->next = NULL;
  156.             isEmptyB = 0;
  157.         }
  158.         prevNode = currNode;                  
  159.     }
  160.     return isEmptyB;
  161. }
  162.  
  163. void ListMerge(struct TList A,struct TList B,struct TList *L)
  164. {
  165.      struct TNode *tmpA, *tmpB;
  166.      struct TNode *poprzedniZtmpA = NULL;
  167.      struct TNode *poprzedniZtmpB = NULL;
  168.      int koniecSeriiWtmpA = 0;
  169.      int koniecSeriiWtmpB = 0;
  170.      ListInit(L);
  171.      if(A.head != NULL)
  172.      {
  173.          // Usun wezel z glowy listy A
  174.          tmpA = A.head;
  175.          if(A.head->next == NULL)
  176.              A.tail = NULL;
  177.          else
  178.              A.head->next->prev = NULL;
  179.          A.head = A.head->next;                      
  180.      }
  181.      if(B.head != NULL)
  182.      {
  183.          // Usun wezel z glowy listy B
  184.          tmpB = B.head;
  185.          if(B.head->next == NULL)
  186.              B.tail = NULL;
  187.          else
  188.              B.head->next->prev = NULL;
  189.          B.head = B.head->next;                    
  190.      }
  191.      while(A.head != NULL || B.head != NULL)
  192.      {
  193.            while(!koniecSeriiWtmpA && !koniecSeriiWtmpB)
  194.                if(tmpA->data > tmpB->data)
  195.                {
  196.                   // Wstaw wezel z listy A za ogon listy L
  197.                   if((*L).head == NULL)
  198.                      (*L).head = tmpA;
  199.                   else
  200.                   {
  201.                       (*L).tail->next = tmpA;
  202.                       tmpA->prev = (*L).tail;
  203.                   }
  204.                   (*L).tail = tmpA;
  205.                   (*L).tail->next = NULL;
  206.                   poprzedniZtmpA = tmpA;
  207.                  // Usun wezel z glowy listy A
  208.                  tmpA = A.head;
  209.                  if(A.head->next == NULL)
  210.                      A.tail = NULL;
  211.                  else
  212.                      A.head->next->prev = NULL;
  213.                  A.head = A.head->next;
  214.                  if(poprzedniZtmpA->data > tmpA->data)
  215.                       koniecSeriiWtmpA = 1;                                  
  216.                }
  217.                else
  218.                {
  219.                    // Wstaw wezel z listy B za ogon listy L
  220.                    if((*L).head == NULL)
  221.                      (*L).head = tmpB;
  222.                    else
  223.                    {
  224.                        (*L).tail->next = tmpB;
  225.                        tmpB->prev = (*L).tail;
  226.                    }
  227.                    (*L).tail = tmpB;
  228.                    (*L).tail->next = NULL;
  229.                    poprzedniZtmpB = tmpB;
  230.                    // Usun wezel z glowy listy B
  231.                    tmpB = B.head;
  232.                    if(B.head->next == NULL)
  233.                        B.tail = NULL;
  234.                    else
  235.                        B.head->next->prev = NULL;
  236.                    B.head = B.head->next;
  237.                    if(poprzedniZtmpB->data > tmpB->data)
  238.                       koniecSeriiWtmpB = 1;
  239.                }
  240.             while(!koniecSeriiWtmpA)
  241.             {
  242.                 // Wstaw wezel z listy A za ogon listy L
  243.                 if((*L).head == NULL)
  244.                      (*L).head = tmpA;
  245.                   else
  246.                   {
  247.                       (*L).tail->next = tmpA;
  248.                       tmpA->prev = (*L).tail;
  249.                   }
  250.                   (*L).tail = tmpA;
  251.                   (*L).tail->next = NULL;
  252.                  poprzedniZtmpA = tmpA;
  253.                // Usun wezel z glowy listy A
  254.                  tmpA = A.head;
  255.                  if(A.head->next == NULL)
  256.                      A.tail = NULL;
  257.                  else
  258.                      A.head->next->prev = NULL;
  259.                  A.head = A.head->next;
  260.                  if(poprzedniZtmpA->data > tmpA->data)
  261.                       koniecSeriiWtmpA = 1;
  262.             }
  263.             while(!koniecSeriiWtmpB)
  264.             {
  265.                 // Wstaw wezel z listy B za ogon listy L
  266.                 if((*L).head == NULL)
  267.                      (*L).head = tmpB;
  268.                   else
  269.                   {
  270.                       (*L).tail->next = tmpB;
  271.                       tmpB->prev = (*L).tail;
  272.                   }
  273.                   (*L).tail = tmpB;
  274.                   (*L).tail->next = NULL;
  275.                 poprzedniZtmpB = tmpB;
  276.                // Usun wezel z glowy listy B
  277.                tmpB = B.head;
  278.                if(B.head->next == NULL)
  279.                    B.tail = NULL;
  280.                else
  281.                    B.head->next->prev = NULL;
  282.                B.head = B.head->next;
  283.                if(poprzedniZtmpB->data > tmpB->data)
  284.                    koniecSeriiWtmpB = 1;
  285.             }
  286.            koniecSeriiWtmpA = 0;
  287.            koniecSeriiWtmpB = 0;
  288.            poprzedniZtmpA = NULL;
  289.            poprzedniZtmpB = NULL;                              
  290.      }
  291. }
  292.  
  293. int main()
  294. {
  295.     int k,n,m;
  296.     struct TList L,L1,L2;
  297.     ListInit(&L);
  298.     srand(time(NULL));
  299.     scanf("%d %d",&n,&m);
  300.     for(k = 1;k <= n;k++)
  301.         ListInsert(&L,rand()%m);
  302.     printf("List L \n");
  303.     ListDisplayForward(L);
  304.     ListDisplayBackward(L);
  305.     ListSplit(L,&L1,&L2);
  306.    printf("List L1 \n");
  307.     ListDisplayForward(L1);
  308.     ListDisplayBackward(L1);
  309.     printf("List L2 \n");
  310.     ListDisplayForward(L2);
  311.     ListDisplayBackward(L2);
  312.     while(!ListIsEmpty(L1))
  313.         ListDelete(&L1);
  314.     while(!ListIsEmpty(L2))
  315.         ListDelete(&L2);
  316.  /*  
  317.   ListMerge(L1,L2,&L);
  318.     printf("List L \n");
  319.     ListDisplayForward(L);
  320.     ListDisplayBackward(L);
  321.     while(!ListIsEmpty(L))
  322.       ListDelete(&L);
  323. */                  
  324.     system("PAUSE");
  325.     return 0;
  326. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement