Advertisement
Guest User

MERGE_WITH_LIST

a guest
Nov 12th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.89 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct list
  5. {
  6.     long long value;
  7.  
  8.     struct list *next;
  9. }list;
  10.  
  11. void push(list **head, int data)
  12. {
  13.     list *tmp = (list*)malloc(sizeof(list));
  14.  
  15.     (*tmp).value = data;
  16.  
  17.     (*tmp).next = *head;
  18.  
  19.     (*head) = tmp;
  20. }
  21.  
  22. long long pop(list **head)
  23. {
  24.     list *prev = NULL;
  25.  
  26.     long long val;
  27.  
  28.     prev = (*head);
  29.  
  30.     val = (*prev).value;
  31.  
  32.     (*head) = (*(*head)).next;
  33.  
  34.     free(prev);
  35.  
  36.     return val;
  37. }
  38.  
  39. list *getLast(list *head)
  40. {
  41.     while((*head).next)
  42.     {
  43.         head = (*head).next;
  44.     }
  45.     return head;
  46. }
  47.  
  48. void merge(list *a, list *b, list **c)
  49. {
  50.     list tmp;
  51.  
  52.     *c = NULL;
  53.  
  54.     if(a == NULL)
  55.     {
  56.         *c = b;
  57.  
  58.         return;
  59.     }
  60.  
  61.     if(b == NULL)
  62.     {
  63.         *c = a;
  64.  
  65.         return;
  66.     }
  67.  
  68.     if((*a).value < (*b).value)
  69.     {
  70.         *c = a;
  71.  
  72.         a = (*a).next;
  73.     }
  74.     else
  75.     {
  76.         *c = b;
  77.  
  78.         b = (*b).next;
  79.     }
  80.  
  81.     tmp.next = *c;
  82.  
  83.     while(a&&b)
  84.     {
  85.         if((*a).value < (*b).value)
  86.         {
  87.             (*(*c)).next = a;
  88.  
  89.             a = (*a).next;
  90.         }
  91.         else
  92.         {
  93.             (*(*c)).next = b;
  94.  
  95.             b = (*b).next;
  96.         }
  97.  
  98.         (*c) = (*(*c)).next;
  99.     }
  100.  
  101.     if(a)
  102.     {
  103.         while(a)
  104.         {
  105.             (*(*c)).next = a;
  106.  
  107.             (*c) = (*(*c)).next;
  108.  
  109.             a = (*a).next;
  110.         }
  111.     }
  112.     if(b)
  113.     {
  114.         while(b)
  115.         {
  116.             (*(*c)).next = b;
  117.  
  118.             (*c) = (*(*c)).next;
  119.  
  120.             b = (*b).next;
  121.         }
  122.     }
  123.  
  124.     *c = tmp.next;
  125. }
  126.  
  127. void split(list *src, list **low, list **high)
  128. {
  129.     list *fast = NULL;
  130.  
  131.     list *slow = NULL;
  132.  
  133.     if(src == NULL || (*src).next == NULL)
  134.     {
  135.         (*low) = src;
  136.  
  137.         (*high) = NULL;
  138.  
  139.         return;
  140.     }
  141.  
  142.     slow = src;
  143.  
  144.     fast = (*src).next;
  145.  
  146.     while(fast != NULL)
  147.     {
  148.         fast = (*fast).next;
  149.  
  150.         if(fast != NULL)
  151.         {
  152.             fast = (*fast).next;
  153.  
  154.             slow = (*slow).next;
  155.         }
  156.     }
  157.  
  158.     (*low) = src;
  159.  
  160.     (*high) = (*slow).next;
  161.  
  162.     (*slow).next = NULL;
  163. }
  164.  
  165. void merGesort(list **head)
  166. {
  167.     list *low = NULL;
  168.  
  169.     list *high = NULL;
  170.  
  171.     if((*head == NULL) || ((*(*head)).next == NULL))
  172.     {
  173.         return;
  174.     }
  175.  
  176.     split(*head, &low, &high);
  177.     merGesort(&low);
  178.     merGesort(&high);
  179.     merge(low, high, head);
  180. }
  181.  
  182. int main(void)
  183. {
  184.     FILE *inp, *out;
  185.  
  186.     long long current, N = 0;
  187.  
  188.     list *head = NULL;
  189.  
  190.     inp = fopen("input.txt", "r");
  191.  
  192.     while(fscanf(inp, "%lld", &current) != EOF)
  193.     {
  194.         push(&head, current);
  195.  
  196.         N++;
  197.     }
  198.  
  199.     fclose(inp);
  200.  
  201.     merGesort(&head);
  202.  
  203.     out = fopen("output.txt", "w");
  204.  
  205.     for(long long i = 0; i < N; i++)
  206.     {
  207.         fprintf(out, "%lld ", pop(&head));
  208.     }
  209.  
  210.     fclose(out);
  211.  
  212.     return 0;
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement