Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.17 KB | None | 0 0
  1. #include <cstdio>
  2. #include <iostream>
  3.  
  4. class List {
  5.   public:
  6.     int value;
  7.     class List *next, *prev;
  8.  
  9.     List() {
  10.         this->value = 0;
  11.         this->next = NULL;
  12.         this->prev = NULL;
  13.     };
  14.  
  15.     int pop() {
  16.         List *aux = this;
  17.  
  18.         while (aux->next->next != NULL)
  19.             aux = aux->next;
  20.  
  21.         int last = aux->next->value;
  22.         delete aux->next;
  23.  
  24.         aux->next = NULL;
  25.         return last;
  26.     };
  27.     void print() {
  28.         List *aux = this;
  29.  
  30.         while (aux != NULL) {
  31.             if (aux->value > 0)
  32.                 printf("%d", aux->value);
  33.  
  34.             if (aux->next != NULL)
  35.                 printf(" ");
  36.  
  37.             aux = aux->next;
  38.         }
  39.     };
  40.     void push(int v) {
  41.         if (this->next == NULL && this->value == 0) {
  42.             this->value = v;
  43.             return;
  44.         }
  45.  
  46.         List *aux = this;
  47.  
  48.         while (aux->next != NULL)
  49.             aux = aux->next;
  50.  
  51.         List *l = new List();
  52.         l->value = v;
  53.         l->prev = aux;
  54.         l->next = NULL;
  55.  
  56.         aux->next = l;
  57.     };
  58. };
  59.  
  60. int getSize(List *lista) {
  61.     float size = 0;
  62.     List *aux = lista;
  63.  
  64.     while (aux != NULL) {
  65.         aux = aux->next;
  66.         size++;
  67.     }
  68.  
  69.     return size;
  70. }
  71.  
  72. List *mergeSort(List *lista) {
  73.     float size = getSize(lista);
  74.  
  75.     List *aux;
  76.     aux = lista;
  77.  
  78.     List *init = new List();
  79.     List *end = new List();
  80.  
  81.     if (size < 2)
  82.         return aux;
  83.  
  84.     if (size < 3) {
  85.         if (aux->value > aux->next->value) {
  86.             init->push(aux->next->value);
  87.             init->push(aux->value);
  88.             return init;
  89.         }
  90.  
  91.         return aux;
  92.     }
  93.  
  94.     int i = 1;
  95.     while (i < size + 1 && aux != NULL) {
  96.         if (i <= size / 2)
  97.             init->push(aux->value);
  98.         else
  99.             end->push(aux->value);
  100.  
  101.         aux = aux->next;
  102.         i++;
  103.     }
  104.  
  105.     init = mergeSort(init);
  106.     end = mergeSort(end);
  107.  
  108.     List *result = new List();
  109.  
  110.     while (init != NULL && end != NULL) {
  111.  
  112.         if (init->value < end->value) {
  113.             result->push(init->value);
  114.             init = init->next;
  115.         } else {
  116.             result->push(end->value);
  117.             end = end->next;
  118.         }
  119.     }
  120.  
  121.     while (init != NULL) {
  122.         result->push(init->value);
  123.         init = init->next;
  124.     }
  125.  
  126.     while (end != NULL) {
  127.         result->push(end->value);
  128.         end = end->next;
  129.     }
  130.  
  131.     return result;
  132. }
  133.  
  134. int main() {
  135.  
  136.     List *lista = new List();
  137.  
  138.     int n;
  139.     while (scanf("%d", &n) != EOF) {
  140.         if (n <= 100000)
  141.             lista->push(n);
  142.     }
  143.  
  144.     lista = mergeSort(lista);
  145.     lista->print();
  146.  
  147.     return 0;
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement