Advertisement
kokokozhina

dynamic_18_?

Apr 11th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.30 KB | None | 0 0
  1. #include <iostream>//bug
  2. #include <stdio.h>
  3.  
  4. using namespace std;
  5.  
  6.  
  7.  
  8. struct list{
  9.     int inf;
  10.     list* next;
  11.     list* prev;
  12. };
  13.  
  14. void init(list *&h, list *&t){
  15.     h = t = NULL;
  16. }
  17.  
  18. void print(list *&t){
  19.     list* p = t;
  20.     while(p){
  21.         cout << p->inf << " ";
  22.         p = p->prev;
  23.     }
  24. }
  25.  
  26. void add_head(list *&h, list *&t, int x){
  27.     list* p = new list;
  28.     p->prev = NULL;
  29.     p->inf = x;
  30.     if(!t){
  31.         p->next = NULL;
  32.         h = t = p;
  33.     }
  34.     else{
  35.         h->prev = p;
  36.         p->prev = t;
  37.         t = p;
  38.     }
  39. }
  40.  
  41. void add_back(list *&h, list *&t, int x){
  42.     list* p = new list;
  43.     p->next = NULL;
  44.     p->inf = x;
  45.     if(!h){
  46.         p->prev = NULL;
  47.         h = t = p;
  48.     }
  49.     else{
  50.         t->next = p;
  51.         p->prev = t;
  52.         t = p;
  53.     }
  54. }
  55.  
  56. void del_all(list *&h, list *&t){
  57.     while(h){
  58.         list* p = h;
  59.         if(p == t)
  60.             h = t = NULL;
  61.         else{
  62.             h = h->next;
  63.             h->prev = NULL;
  64.         }
  65.         delete p;
  66.     }
  67. }
  68.  
  69. list* find(list *&h, list *&t, int x){
  70.     list* p = h;
  71.     while(p){
  72.         if(p->inf == x) break;
  73.         p = p->next;
  74.     }
  75.     return p;
  76. }
  77.  
  78. void insert_after(list *&h, list *&t, list *&r, int x){//?
  79.     list* p = new list;
  80.     p->inf = x;
  81.     p->prev = r;
  82.     p->next = r->next;
  83.     if(r == t){
  84.         t->next = p;
  85.         r = p;//?
  86.     }
  87.     else{
  88.         (r->next)->prev = p;
  89.         r->next = p;
  90.     }
  91. }
  92.  
  93. int pop_front(list *&h, list *&t){
  94.     int i = h->inf;
  95.     list *r = h;
  96.     h = h->next;
  97.     if(h) h->prev = NULL;
  98.     if(!h) t = NULL;
  99.     delete r;
  100.     return i;
  101. }
  102.  
  103. int pop_back(list *&h, list *&t){
  104.     int i = t->inf;
  105.     list *r = t;
  106.     t = t->prev;
  107.     if(t) t->next = NULL;
  108.     if(!t) h = NULL;
  109.     delete r;
  110.     return i;
  111. }
  112.  
  113. void erase(list *&h, list *&t, list *&r){
  114.     if(r == h && r == t)
  115.         h = t = NULL;
  116.     else if(r == h){
  117.             h = h->next;
  118.             t->next = NULL;
  119.         }
  120.         else if(r == t){
  121.             t = t->prev;
  122.             t->next = NULL;
  123.         }
  124.         else{
  125.             (r->prev)->next = r->next;
  126.             (r->next)->prev = r->prev;
  127.         }
  128.         delete r;
  129. }
  130. //sort list, using stable qsort
  131.  
  132. void connect(list *&auxt, list *&sup){
  133.     auxt->next = sup;
  134.     sup->prev = auxt;
  135. }
  136.  
  137.  
  138.  
  139. void sort(list *&h, list *&t, list *&ansh){
  140.     cout << h->inf << " " << t->inf << endl;
  141.     if(h != t)
  142.     {
  143.         list *sup = h;//support list
  144.         list *auxh, *auxt;
  145.         init(auxh, auxt);
  146.         list *cur = h->next;//current element
  147.         while(cur){
  148.             if(cur->inf < sup->inf){
  149.                 add_back(auxh, auxt, cur->inf);
  150.                 list *ers = cur;
  151.                 cur = cur->next;
  152.                 erase(h, t, ers);
  153.             }
  154.             else cur = cur->next;
  155.         }
  156.         cout << "ok\n";
  157.         if(auxh && auxt){
  158.             connect(auxt, sup);
  159.             //print(auxt); cout << endl; print(t); cout << endl;
  160.             if(!ansh || auxh->inf < ansh->inf){
  161.                 ansh = auxh;
  162.             }
  163.             sort(auxh, auxt, ansh);
  164.             sort(sup, t, ansh);
  165.         }
  166.         else{
  167.             cout << "new iter\n";
  168.             if(sup->next) sort(sup->next, t, ansh);
  169.            
  170.         }
  171.     }
  172.     cout << ansh->inf << "woow\n";
  173.     while(ansh){
  174.         cout << ansh->inf << endl;
  175.         ansh = ansh->next;
  176.     }
  177.     cout << "woow\n";
  178.     if(ansh->prev != h) connect(ansh->prev, h);
  179. }
  180.  
  181. int main()
  182. {
  183. #ifdef _DEBUG
  184.     freopen("input.txt", "r", stdin);
  185.     freopen("output.txt", "w", stdout);
  186. #endif
  187.  
  188.     printf("Enter the number of elements and new member, then array of numbers\n");
  189.     int n; scanf("%d", &n);
  190.     list *h, *t;
  191.     init(h, t);
  192.  
  193.     for(int i = 0; i < n; i++){
  194.         int cur; scanf("%d", &cur);
  195.         add_back(h, t, cur);
  196.     }
  197.     list *ansh = h;
  198.  
  199.     sort(h, t, ansh);
  200.     while(ansh){
  201.         cout << ansh->inf << " ";
  202.         ansh = ansh->next;
  203.     }
  204.     //cout << ansh->inf << " ";
  205.     return 0;
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement