Guest User

Split Linked List for Natural Merge Sort with helper

a guest
Mar 24th, 2019
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.72 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. struct TNode *headdel(struct TNode **first,struct TNode **last)
  89. {
  90.     struct TNode *temp = (*first);
  91.     if((*first) != NULL)
  92.     {
  93.         if((*first)->next == NULL)
  94.             (*last) = NULL;
  95.         else
  96.             (*first)->next->prev = NULL;
  97.         (*first) = (*first)->next;                      
  98.     }
  99.     return temp;  
  100. }
  101.  
  102. void tailins(struct TNode *p,struct TNode **first,struct TNode **last)
  103. {
  104.      p->next = NULL;
  105.      if((*first) == NULL)
  106.      {
  107.          p->prev = NULL;
  108.          (*first) = p;        
  109.      }
  110.      else
  111.      {
  112.          (*last)->next = p;
  113.          p->prev = (*last);
  114.      }
  115.      (*last) = p;
  116. }
  117.  
  118. int ListSplit(struct TList L,struct TList *A,struct TList *B)
  119. {
  120.     int isEmptyB,swapLists;
  121.     struct TNode *currNode,*prevNode;
  122.     swapLists = 1;
  123.     isEmptyB = 1;
  124.     ListInit(A);
  125.     ListInit(B);
  126.     if(L.head != NULL)
  127.     {        
  128.         // Usun wezel z glowy listy L
  129.         //Wstaw wezel za ogon listy A
  130.         currNode = headdel(&L.head,&L.tail);
  131.         tailins(currNode,&(*A).head,&(*A).tail);
  132.         prevNode = currNode;
  133.     }
  134.     while(L.head != NULL)
  135.     {
  136.         // Usun wezel z glowy listy L
  137.         currNode = headdel(&L.head,&L.tail);
  138.         if(prevNode->data > currNode->data)
  139.            swapLists = !swapLists;
  140.         if(swapLists)
  141.         {
  142.            //Wstaw wezel za ogon listy A
  143.            tailins(currNode,&(*A).head,&(*A).tail);
  144.         }
  145.         else
  146.         {
  147.             //Wstaw wezel za ogon listy B
  148.             tailins(currNode,&(*B).head,&(*B).tail);
  149.             isEmptyB = 0;
  150.         }
  151.         prevNode = currNode;                  
  152.     }
  153.     return isEmptyB;
  154. }
  155.  
  156. void ListMerge(struct TList A,struct TList B,struct TList *L)
  157. {
  158.      struct TNode *tmpA, *tmpB;
  159.      struct TNode *poprzedniZtmpA = NULL;
  160.      struct TNode *poprzedniZtmpB = NULL;
  161.      int koniecSeriiWtmpA = 0;
  162.      int koniecSeriiWtmpB = 0;
  163.      ListInit(L);
  164.      if(A.head != NULL)
  165.      {
  166.          // Usun wezel z glowy listy A
  167.          tmpA = headdel(&A.head,&A.tail);                      
  168.      }
  169.      if(B.head != NULL)
  170.      {
  171.          // Usun wezel z glowy listy B
  172.          tmpB = headdel(&B.head,&B.tail);                    
  173.      }
  174.      while(A.head != NULL || B.head != NULL)
  175.      {
  176.            while(!koniecSeriiWtmpA && !koniecSeriiWtmpB)
  177.                if(tmpA->data > tmpB->data)
  178.                {
  179.                   // Wstaw wezel z listy A za ogon listy L
  180.                   tailins(tmpA,&(*L).head,&(*L).tail);
  181.                   poprzedniZtmpA = tmpA;
  182.                  // Usun wezel z glowy listy A
  183.                  tmpA = headdel(&A.head,&A.tail);
  184.                  if(poprzedniZtmpA->data > tmpA->data)
  185.                       koniecSeriiWtmpA = 1;                                  
  186.                }
  187.                else
  188.                {
  189.                    // Wstaw wezel z listy B za ogon listy L
  190.                    tailins(tmpB,&(*L).head,&(*L).tail);
  191.                    poprzedniZtmpB = tmpB;
  192.                    // Usun wezel z glowy listy B
  193.                    tmpB = headdel(&B.head,&B.tail);
  194.                    if(poprzedniZtmpB->data > tmpB->data)
  195.                       koniecSeriiWtmpB = 1;
  196.                }
  197.             while(!koniecSeriiWtmpA)
  198.             {
  199.                 // Wstaw wezel z listy A za ogon listy L
  200.                 tailins(tmpA,&(*L).head,&(*L).tail);
  201.                  poprzedniZtmpA = tmpA;
  202.                // Usun wezel z glowy listy A
  203.                  tmpA = headdel(&A.head,&A.tail);
  204.                  if(poprzedniZtmpA->data > tmpA->data)
  205.                       koniecSeriiWtmpA = 1;
  206.             }
  207.             while(!koniecSeriiWtmpB)
  208.             {
  209.                 // Wstaw wezel z listy B za ogon listy L
  210.                 tailins(tmpB,&(*L).head,&(*L).tail);
  211.                 poprzedniZtmpB = tmpB;
  212.                // Usun wezel z glowy listy B
  213.                tmpB = headdel(&B.head,&B.tail);
  214.                if(poprzedniZtmpB->data > tmpB->data)
  215.                    koniecSeriiWtmpB = 1;
  216.             }
  217.            koniecSeriiWtmpA = 0;
  218.            koniecSeriiWtmpB = 0;
  219.            poprzedniZtmpA = NULL;
  220.            poprzedniZtmpB = NULL;                              
  221.      }
  222. }
  223.  
  224. int main()
  225. {
  226.     int k,n,m;
  227.     struct TList L,L1,L2;
  228.     ListInit(&L);
  229.     srand(time(NULL));
  230.     scanf("%d %d",&n,&m);
  231.     for(k = 1;k <= n;k++)
  232.         ListInsert(&L,rand()%m);
  233.     printf("List L \n");
  234.     ListDisplayForward(L);
  235.     ListDisplayBackward(L);
  236.     ListSplit(L,&L1,&L2);
  237.    printf("List L1 \n");
  238.     ListDisplayForward(L1);
  239.     ListDisplayBackward(L1);
  240.     printf("List L2 \n");
  241.     ListDisplayForward(L2);
  242.     ListDisplayBackward(L2);
  243.     while(!ListIsEmpty(L1))
  244.         ListDelete(&L1);
  245.     while(!ListIsEmpty(L2))
  246.         ListDelete(&L2);
  247.  /*
  248.     ListMerge(L1,L2,&L);
  249.     printf("List L \n");
  250.     ListDisplayForward(L);
  251.     ListDisplayBackward(L);
  252.     while(!ListIsEmpty(L))
  253.       ListDelete(&L);  
  254.       */                
  255.     system("PAUSE");
  256.     return 0;
  257. }
Advertisement
Add Comment
Please, Sign In to add comment