Created
May 9, 2017 15:55
-
-
Save tornikegomareli/68e17f5da18b661d0df9a08fb4f57490 to your computer and use it in GitHub Desktop.
Implementation of QueuePriority Data Structure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <string.h> | |
| #include <time.h> | |
| using namespace std; | |
| class QueuePriority | |
| { | |
| int * Wait; | |
| int * Pri; | |
| int MaxQueueLength; | |
| int QueueLength; | |
| public: | |
| QueuePriority(int m); | |
| ~QueuePriority(); | |
| void Add(int c, int p); | |
| int Extract(); | |
| void Clear(); | |
| bool IsEmpty(); | |
| bool IsFull(); | |
| int GetCount(); | |
| void Show(); | |
| }; | |
| void QueuePriority::Show() { | |
| cout << "\n-------------------------------------\n"; | |
| for (int i = 0; i<QueueLength; i++) { | |
| cout << Wait[i] << " - " << Pri[i] << "\n\n"; | |
| } | |
| cout << "\n-------------------------------------\n"; | |
| } | |
| QueuePriority::~QueuePriority() | |
| { | |
| delete[]Wait; | |
| delete[]Pri; | |
| } | |
| QueuePriority::QueuePriority(int m) | |
| { | |
| MaxQueueLength = m; | |
| // რიგის შექმნა | |
| Wait = new int[MaxQueueLength]; | |
| Pri = new int[MaxQueueLength]; | |
| // დასაწყისში რიგი ცარიელია | |
| QueueLength = 0; | |
| } | |
| void QueuePriority::Clear() | |
| { | |
| QueueLength = 0; | |
| } | |
| bool QueuePriority::IsEmpty() | |
| { | |
| return QueueLength == 0; | |
| } | |
| bool QueuePriority::IsFull() | |
| { | |
| return QueueLength == MaxQueueLength; | |
| } | |
| int QueuePriority::GetCount() | |
| { | |
| return QueueLength; | |
| } | |
| void QueuePriority::Add(int c, int p) | |
| { | |
| // თუ რიგი არაა სავსე გაზარდე ელემენტების რაოდენობა | |
| // და დაამატე ახალი ელემენტი | |
| if (!IsFull()) { | |
| Wait[QueueLength] = c; | |
| Pri[QueueLength] = p; | |
| QueueLength++; | |
| } | |
| } | |
| int QueuePriority::Extract() | |
| { | |
| if (!IsEmpty()) { | |
| // დავუშვათ მაქსიმალური პრიორიტეტი აქვს ნულოვან ელემენტს | |
| int max_pri = Pri[0]; | |
| // ხოლო მისი ინდექსი = 0 | |
| int pos_max_pri = 0; | |
| // ვეძებთ მაქსიმალურ პრიორიტეტს | |
| for (int i = 1; i<QueueLength; i++) | |
| // თუ მივადგებით უფრო პრიორიტეტულ ელემენტს | |
| if (max_pri<Pri[i]) { | |
| max_pri = Pri[i]; | |
| pos_max_pri = i; | |
| } | |
| // ამოიღოთ პრიორიტეტული ელემენტი | |
| int temp1 = Wait[pos_max_pri]; | |
| int temp2 = Pri[pos_max_pri]; | |
| //წავანაცვლოთ ყველა ელემენტი | |
| for (int i = pos_max_pri; i<QueueLength - 1; i++) { | |
| Wait[i] = Wait[i + 1]; | |
| Pri[i] = Pri[i + 1]; | |
| } | |
| // შევამციროთ რაოდენობა | |
| QueueLength--; | |
| // დავაბრუნოთ ამოღებული ელემენტი | |
| return temp1; | |
| } | |
| else return -1; | |
| } | |
| void main() | |
| { | |
| srand(time(0)); | |
| QueuePriority QUP(25); | |
| for (int i = 0; i<5; i++) { | |
| QUP.Add(rand() % 100, rand() % 12); | |
| } | |
| QUP.Show(); | |
| QUP.Extract(); | |
| QUP.Show(); | |
| cin.get(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment