mramine364

LinkedList.cpp

Sep 4th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.95 KB | None | 0 0
  1. #include "LinkedList.h"
  2.  
  3.  
  4. LinkedList::LinkedList()
  5. {
  6.     start = end = NULL;
  7.     size = 0;
  8. }
  9.  
  10.  
  11. LinkedList::~LinkedList()
  12. {
  13.     while (start != NULL){
  14.         node* n = start;
  15.         start = start->next;
  16.         delete n;
  17.     }
  18. }
  19.  
  20. ostream& operator<<(ostream& out, const LinkedList& l){
  21.     LinkedList::node* s = l.start;
  22.     out << "[";
  23.     if (s == NULL){
  24.         out << "]";
  25.         return out;
  26.     }
  27.     while (s->next != NULL){
  28.         out << s->x << ",";
  29.         s = s->next;
  30.     }
  31.     out << s->x << "]";
  32.     return out;
  33. }
  34.  
  35. int LinkedList::push(int x){
  36.     if (end != NULL){
  37.         node* n = new node;
  38.         n->x = x;
  39.         n->next = NULL;
  40.         n->prev = end;
  41.         end->next = n;
  42.         end = n;
  43.     }
  44.     else{
  45.         node* n = new node;
  46.         n->x = x;
  47.         n->next = NULL;
  48.         n->prev = NULL;
  49.         start = end = n;
  50.     }
  51.     /*cout << "start, end = " << start << "," << end << endl;
  52.     if (start!=NULL)
  53.         cout << "startx = " << start->x;
  54.     if (end != NULL)
  55.         cout << ", endx = " << end->x << endl;
  56.     else
  57.         cout << endl;*/
  58.     return ++size;
  59. }
  60. int LinkedList::pop(){
  61.     if (size == 0)
  62.         return NULL;
  63.     if (size == 1){
  64.         int n = end->x;
  65.         delete end;
  66.         end = start = NULL;
  67.         size--;
  68.         return n;
  69.     }
  70.     int n = end->x;
  71.     node* tmp = end;
  72.     end = end->prev;
  73.     size--;
  74.     delete tmp;
  75.     end->next = NULL;
  76.     return n;
  77. }
  78. int LinkedList::unshift(int x){
  79.     if (end != NULL){
  80.         node* n = new node;
  81.         n->x = x;
  82.         n->next = start;
  83.         n->prev = NULL;
  84.         start->prev = n;
  85.         start = n;
  86.     }
  87.     else{
  88.         node* n = new node;
  89.         n->x = x;
  90.         n->next = NULL;
  91.         n->prev = NULL;
  92.         start = end = n;
  93.     }
  94.     return ++size;
  95. }
  96. int LinkedList::shift(){
  97.     if (size == 0)
  98.         return NULL;
  99.     if (size == 1){
  100.         int n = end->x;
  101.         delete end;
  102.         end = start = NULL;
  103.         size--;
  104.         return n;
  105.     }
  106.     int n = start->x;
  107.     node* tmp = start;
  108.     start = start->next;
  109.     size--;
  110.     delete tmp;
  111.     start->prev = NULL;
  112.     return n;
  113. }
  114.  
  115. int LinkedList::find(int x){
  116.     int r = 0;
  117.     node* s = start;
  118.     while (s != NULL){
  119.         if (s->x == x){
  120.             return r;
  121.         }
  122.         r++;
  123.         s = s->next;
  124.     }
  125.     return -1;
  126. }
  127. LinkedList* LinkedList::copy(int s, int e){
  128.     LinkedList* r = new LinkedList;
  129.     int i = 0;
  130.     node* st = start;
  131.     while (st != NULL && i < s){
  132.         st = st->next;
  133.         i++;
  134.     }
  135.     if (st == NULL)
  136.         return r;
  137.     while (st != NULL && i < e){
  138.         r->push(st->x);
  139.         st = st->next;
  140.         i++;
  141.     }
  142.     return r;
  143. }
  144. LinkedList* LinkedList::splice(int s, int e){
  145.     LinkedList* r = new LinkedList;
  146.     int i = 0;
  147.     node* st = start;
  148.     while (st != NULL && i < s){
  149.         st = st->next;
  150.         i++;
  151.     }
  152.     if (st == NULL)
  153.         return r;
  154.     node* tm = st;
  155.     while (st != NULL && i < e){
  156.         r->push(st->x);
  157.         node* tmp = st;
  158.         if (st == start){
  159.             start = st = st->next;
  160.             delete tmp;
  161.             start->prev = NULL;
  162.         }
  163.         else if (st == end){
  164.             node* tn = st->prev;           
  165.             st = NULL;
  166.             delete tmp;
  167.             tn->next = NULL;
  168.         }
  169.         else{
  170.             st->next->prev = st->prev;
  171.             st->prev->next = st->next;
  172.             st = st->next;
  173.             delete tmp;
  174.         }      
  175.         i++;
  176.         size--;
  177.     }
  178.     return r;
  179. }
  180.  
  181. LinkedList::node* LinkedList::_sort(node* r, int n){ //return the first node
  182.     if (n == 0)
  183.         return NULL;
  184.     if (n == 1)
  185.         return r;
  186.     if (n == 2){
  187.         if (r->x > r->next->x)
  188.             _swap(r, r->next);
  189.         return r;
  190.     }
  191.  
  192.     bool s1 = true, s2 = true;
  193.     int i = 0;
  194.     node *tr = r, *f, *p;
  195.     while (i<(n - 1) / 2){
  196.         if (tr->x > tr->next->x)
  197.             s1 = false;
  198.         tr = tr->next;
  199.         i++;
  200.     }
  201.     tr = tr->next;
  202.     i++;
  203.     int ti = i;
  204.     node* r2 = tr;
  205.     while (ti<n - 1){
  206.         if (tr->x > tr->next->x){
  207.             s2 = false;
  208.             break;
  209.         }
  210.         tr = tr->next;
  211.         ti++;
  212.     }
  213.     if (!s1)
  214.         r = _sort(r, i);
  215.     if (!s2)
  216.         r2 = _sort(r2, n - i);
  217.  
  218.     node* first = r;
  219.     f = r;
  220.     p = r2->prev;
  221.  
  222.     ti = i;
  223.     int k = 0;
  224.     while (i < n && k<ti){
  225.         if (r->x > r2->x){
  226.             p->next = r2->next;
  227.             if (r2->next != NULL)
  228.                 r2->next->prev = p;
  229.             r2->next = r;
  230.             r2->prev = r->prev;
  231.             r->prev = r2;
  232.  
  233.             if (f != r){
  234.                 f->next = r2;
  235.                 f = f->next;
  236.             }
  237.             else{
  238.                 first = r2;
  239.                 f = r2;
  240.             }
  241.             r2 = p->next;
  242.             i++;
  243.         }
  244.         else{
  245.             if (r != f)
  246.                 f = f->next;
  247.             r = r->next;
  248.             k++;
  249.         }
  250.     }
  251.     return first;
  252. }
Add Comment
Please, Sign In to add comment