Created
May 5, 2017 16:02
-
-
Save tornikegomareli/057bf4c6caaf19759749b411ea84e5f6 to your computer and use it in GitHub Desktop.
Queue Data Structure Implementation with sample Array
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 Queue | |
| { | |
| // რიგი | |
| int * Que; | |
| // რიგის მაქსიმალური სიგრძე | |
| int MaxQueueLength; | |
| // რიგის მიმდინარე ზომა | |
| int QueueLength; | |
| public: | |
| Queue(int m); | |
| ~Queue(); | |
| void Add(int c); | |
| int Extract(); | |
| void Clear(); | |
| bool IsEmpty(); | |
| bool IsFull(); | |
| int GetCount(); | |
| void Show(); | |
| }; | |
| void Queue::Show() { | |
| cout << "\n-------------------------------------\n"; | |
| //რიგის ჩვენება | |
| for (int i = 0; i<QueueLength; i++) { | |
| cout << Que[i] << " "; | |
| } | |
| cout << "\n-------------------------------------\n"; | |
| } | |
| Queue::~Queue() | |
| { | |
| // რიგის წაშლა | |
| delete[]Que; | |
| } | |
| Queue::Queue(int m) | |
| { | |
| // ზომის გადაცემა | |
| MaxQueueLength = m; | |
| // რიგის შექმნა | |
| Que = new int[MaxQueueLength]; | |
| // თავდაპირველად რიგი ცარიელია | |
| QueueLength = 0; | |
| } | |
| void Queue::Clear() | |
| { | |
| QueueLength = 0; | |
| } | |
| bool Queue::IsEmpty() | |
| { | |
| return QueueLength == 0; | |
| } | |
| bool Queue::IsFull() | |
| { | |
| return QueueLength == MaxQueueLength; | |
| } | |
| int Queue::GetCount() | |
| { | |
| return QueueLength; | |
| } | |
| void Queue::Add(int c) | |
| { | |
| if (!IsFull()) | |
| Que[QueueLength++] = c; | |
| } | |
| int Queue::Extract() | |
| { | |
| if (!IsEmpty()) { | |
| // პირველის დამახსოვრება | |
| int temp = Que[0]; | |
| // ყველა ელემენტის წანაცვლება | |
| for (int i = 1; i<QueueLength; i++) | |
| Que[i - 1] = Que[i]; | |
| //რაოდენობის შემცირება | |
| QueueLength--; | |
| //პირველი (ნულოვანი) ელემენტის დაბრუნება | |
| return temp; | |
| } | |
| else | |
| return -1; | |
| } | |
| void main() | |
| { | |
| srand(time(0)); | |
| Queue QU(25); | |
| for (int i = 0; i<5; i++) { | |
| QU.Add(rand() % 50); | |
| } | |
| QU.Show(); | |
| QU.Extract(); | |
| QU.Extract(); | |
| QU.Show(); | |
| cin.get(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment