Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #include <stack>
  4.  
  5. using namespace std;
  6.  
  7. struct elem
  8. {
  9.     int data;
  10.     unsigned int priority;
  11.  
  12.     elem *next, *prev;
  13. };
  14.  
  15. struct priority_queue1
  16. {
  17.     int size = 0;
  18.     elem *back, *front;
  19.  
  20.     priority_queue1(void) : back(NULL)
  21.     {}
  22. };
  23.  
  24.  
  25. bool pop(priority_queue1 &q, int &out, unsigned &pr)
  26. {
  27.     if (!q.back) return false;
  28.     q.size--;
  29.  
  30.     if (!q.back->next)
  31.     {
  32.         pr = q.back->priority;
  33.         out = q.back->data;
  34.         delete q.back;
  35.         q.back = NULL;
  36.         return true;
  37.     }
  38.  
  39.     elem *prev, *end;
  40.     prev = end = q.back;
  41.     while (end->next)
  42.     {
  43.         prev = end;
  44.         end = end->next;
  45.     }
  46.     pr = end->priority;
  47.     out = end->data;
  48.     delete end;
  49.     prev->next = NULL;
  50.     return true;
  51. }
  52.  
  53. void clear(priority_queue1 &q)
  54. {
  55.     elem *del;
  56.     while (q.back)
  57.     {
  58.         del = q.back;
  59.         q.back = q.back->next;
  60.         delete del;
  61.     }
  62. }
  63.  
  64. void print(priority_queue1 &q)
  65. {
  66.     elem *cursor = q.back;
  67.     while (cursor)
  68.     {
  69.         std::cout << cursor->data << " ";
  70.         cursor = cursor->next;
  71.     }
  72.     std::cout << std::endl;
  73. }
  74.  
  75. bool compare(elem *prev, elem *next)
  76. {
  77.     return prev->priority > next->priority;
  78. }
  79.  
  80.  
  81. void push_by_prior(priority_queue1 &q, const int &value, const unsigned int &priority_value)
  82. {
  83.     q.size++;
  84.  
  85.     elem *temp = new elem;
  86.     temp->data = value;
  87.     temp->priority = priority_value;
  88.     temp->next = NULL;
  89.  
  90.     if (!q.back)
  91.     {
  92.         q.back = temp;
  93.         return;
  94.     }
  95.  
  96.     elem *p = q.back, *prev = NULL;
  97.     for (; p && compare(temp, p); prev = p, p = p->next);
  98.  
  99.     if (p == q.back)
  100.     {
  101.         temp->next = q.back;
  102.         q.back = temp;
  103.         return;
  104.     }
  105.  
  106.     prev->next = temp;
  107.     if (!p)return;
  108.  
  109.     temp->next = p;
  110. }
  111.  
  112.  
  113. void newn(priority_queue1 &q, priority_queue1 &a)
  114. {
  115.     unsigned prior;
  116.     int x;
  117.  
  118.     int i = 1;
  119.     while (q.size > 0)
  120.     {
  121.         if (i % 2 == 0)
  122.         {
  123.             pop(q, x, prior);
  124.             prior = prior - 1;
  125.             push_by_prior(a, x, prior);
  126.         }
  127.         else
  128.         {
  129.             pop(q, x, prior);
  130.             push_by_prior(a, x, prior);
  131.         }
  132.         i++;
  133.     }
  134. }
  135.  
  136.  
  137. int main()
  138. {
  139.     priority_queue1 q, a;
  140.     unsigned prior;
  141.  
  142.  
  143.     push_by_prior(q, 3, 2);
  144.     push_by_prior(q, 5, 4);
  145.     push_by_prior(q, 7, 3);
  146.     push_by_prior(q, 12, 5);
  147.     push_by_prior(q, 2, 2);
  148.  
  149.     int value = 0;
  150.  
  151. /*
  152.     if (pop(q, value, prior))std::cout << value << " ";
  153.     if (pop(q, value, prior))std::cout << value << " ";
  154.     if (pop(q, value, prior))std::cout << value << " ";
  155.     if (pop(q, value, prior))std::cout << value << " ";
  156. */
  157.  
  158.  
  159.     print(q);
  160.     newn(q, a);
  161.     value = 0;
  162.  
  163.     if (pop(a, value, prior))std::cout << value << " ";
  164.     if (pop(a, value, prior))std::cout << value << " ";
  165.     if (pop(a, value, prior))std::cout << value << " ";
  166.     if (pop(a, value, prior))std::cout << value << " ";
  167.  
  168.  
  169.     print(a);
  170.  
  171.  
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement