Advertisement
Guest User

Untitled

a guest
Feb 18th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.47 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. template <typename Tvalue, typename Pvalue>
  8. class QueuePriority
  9. {
  10.     // Tvalue - არის ვეითერებისთვის
  11.     // Pvalue - არის პრიორიტეტებისთვის.
  12. private:
  13.     int* waiters;
  14.     int* priorities;
  15.     int maxQueueLength;
  16.     int queueLength;
  17. public:
  18.  
  19.     QueuePriority() = default;
  20.  
  21.     QueuePriority(int size)
  22.     {
  23.         maxQueueLength = size;
  24.         waiters = new int[maxQueueLength];
  25.         priorities = new int[maxQueueLength];
  26.         queueLength = 0;
  27.     }
  28.  
  29.     void Enqueue(int data, int priorit)
  30.     {
  31.         if (waiters == nullptr || priorities == nullptr)
  32.             return;
  33.  
  34.         if (IsFull())
  35.             return;
  36.  
  37.         waiters[queueLength] = data;
  38.         priorities[queueLength] = priorit;
  39.  
  40.         queueLength++;
  41.     }
  42.  
  43.  
  44.     int Dequeue()
  45.     {
  46.         if (waiters == nullptr || priorities == nullptr)
  47.             return -1;
  48.  
  49.         if (IsEmpty())
  50.             return -1;
  51.  
  52.         int max_pri = priorities[0];
  53.         int pos_max_pri = 0;
  54.  
  55.         for (int i = 0; i < queueLength; i++)
  56.         {
  57.             if (priorities[i] > max_pri)
  58.             {
  59.                 max_pri = priorities[i];
  60.                 pos_max_pri = i;
  61.             }
  62.         }
  63.  
  64.         int waiterToDequeue = waiters[pos_max_pri];
  65.         int priorityToDequeue = priorities[pos_max_pri];
  66.  
  67.         for (int i = pos_max_pri; i < queueLength; i++)
  68.         {
  69.             waiters[i] = waiters[i + 1];
  70.             priorities[i] = priorities[i + 1];
  71.         }
  72.  
  73.         waiters[queueLength] = NULL;
  74.         priorities[queueLength] = NULL;
  75.  
  76.         queueLength--;
  77.  
  78.         return waiterToDequeue;
  79.     }
  80.    
  81.     void Clear()
  82.     {
  83.         if (waiters != nullptr)
  84.             delete[] waiters;
  85.  
  86.         if (priorities != nullptr)
  87.             delete[] priorities;
  88.  
  89.         waiters = new int[maxQueueLength];
  90.         priorities = new int[maxQueueLength];
  91.  
  92.         queueLength = 0;
  93.     }
  94.  
  95.     bool IsEmpty()
  96.     {
  97.         return queueLength == 0;
  98.     }
  99.  
  100.     bool IsFull()
  101.     {
  102.         return queueLength == maxQueueLength;
  103.     }
  104.  
  105.     int Count()
  106.     {
  107.         return queueLength;
  108.     }
  109.  
  110.     void ShowQueue()
  111.     {
  112.         cout << " ------------------- " << endl;
  113.         cout << "---------Values---------------- " << endl;
  114.         for (int i = 0; i < queueLength; i++)
  115.         {
  116.             cout << waiters[i] << " ";
  117.         }
  118.         cout << endl;
  119.         cout << "---------Priorities---------------- " << endl;
  120.         for (int i = 0; i < queueLength; i++)
  121.         {
  122.             cout << priorities[i] << " ";
  123.         }
  124.         cout << " ------------------- " << endl;
  125.     }
  126.  
  127.     ~QueuePriority()
  128.     {
  129.         if(waiters != nullptr)
  130.             delete[] waiters;
  131.  
  132.         if(priorities != nullptr)
  133.             delete[] priorities;
  134.     }
  135. };
  136.  
  137.  
  138. int main()
  139. {
  140.  
  141.    
  142.  
  143.    
  144.  
  145.  
  146.  
  147.  
  148.     cin.get();
  149.     cin.get();
  150.     return 0;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement