Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
136
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.         printf("\n");
  31.         while (aux != NULL) {
  32.             printf("%d", aux->value);
  33.             if (aux->next != NULL)
  34.                 printf(" ");
  35.             aux = aux->next;
  36.         }
  37.         printf("\n");
  38.     };
  39.     void push(int v) {
  40.         if (this->next == NULL && this->value == 0) {
  41.             this->value = v;
  42.             return;
  43.         }
  44.  
  45.         List *aux = this;
  46.  
  47.         while (aux->next != NULL)
  48.             aux = aux->next;
  49.  
  50.         List *l = new List();
  51.         l->value = v;
  52.         l->prev = aux;
  53.         l->next = NULL;
  54.  
  55.         aux->next = l;
  56.     };
  57. };
  58.  
  59. int getSize(List *lista) {
  60.     float size = 0;
  61.     List *aux = lista;
  62.  
  63.     while (aux != NULL) {
  64.         aux = aux->next;
  65.         size++;
  66.     }
  67.  
  68.     return size;
  69. }
  70.  
  71. List *mergeSort(List *lista) {
  72.     float size = getSize(lista);
  73.  
  74.     List *aux;
  75.     aux = lista;
  76.  
  77.     List *init = new List();
  78.     List *end = new List();
  79.  
  80.     if (size < 2)
  81.         return aux;
  82.  
  83.     if (size < 3) {
  84.         if (aux->value > aux->next->value) {
  85.             init->push(aux->next->value);
  86.             init->push(aux->value);
  87.             return init;
  88.         }
  89.  
  90.         return aux;
  91.     }
  92.  
  93.     int i = 1;
  94.     while (i < size + 1 && aux != NULL) {
  95.         if (i <= size / 2)
  96.             init->push(aux->value);
  97.         else
  98.             end->push(aux->value);
  99.  
  100.         aux = aux->next;
  101.         i++;
  102.     }
  103.  
  104.     init = mergeSort(init);
  105.     end = mergeSort(end);
  106.  
  107.     List *result = new List();
  108.  
  109.     while (init != NULL && end != NULL) {
  110.  
  111.         if (init->value < end->value) {
  112.             result->push(init->value);
  113.             init = init->next;
  114.         } else {
  115.             result->push(end->value);
  116.             end = end->next;
  117.         }
  118.     }
  119.  
  120.     while (init != NULL) {
  121.         result->push(init->value);
  122.         init = init->next;
  123.     }
  124.  
  125.     while (end != NULL) {
  126.         result->push(end->value);
  127.         end = end->next;
  128.     }
  129.  
  130.     return result;
  131. }
  132.  
  133. int main() {
  134.  
  135.     List *lista = new List();
  136.  
  137.     int n = 0;
  138.     while (scanf("%d", &n) != EOF)
  139.         if (n < 100000)
  140.             lista->push(n);
  141.  
  142.     lista = mergeSort(lista);
  143.     lista->print();
  144.  
  145.     return 0;
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement