-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriorityQueue.cpp
More file actions
212 lines (184 loc) · 5.12 KB
/
priorityQueue.cpp
File metadata and controls
212 lines (184 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
* priorityQueue.cpp
*
* Copyright 2021 raja <raja@raja-Inspiron-N5110>
*
* This queueogram is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This queueogram is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this queueogram; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
*/
#include "priorityQueue.h"
using namespace std;
// Step 1 : As per the requirement, we need a storage class with two fields -
// Hence we can make use of the structures concept to create elements which act as Input
struct InPut {
int priority;
string value;
};
// Pointer to the last lastIndex
int lastIndex = -1;
// Store the element of a queue priority queue
InPut priorityQ[100];
class Priority_Q {
public:
Priority_Q()
{
}
// Function to insert a new element into priority queue
void insertToQueue(int priority ,string value)
{
// Increase the lastIndex
lastIndex++;
// Insert the element into the queue
priorityQ[lastIndex].value = value;
priorityQ[lastIndex].priority = priority;
}
// Function to check the element with maxium priority
int getmaxpriority(void)
{
int maxpriority = INT_MIN;
int ind = -1;
// Check for the element with maximum priority
for (int i = 0; i <= lastIndex; i++) {
// Handling edge case of threads with equal priorities : Based on Accept / Decline.
// precedence is given to values of Accept.
if (maxpriority == priorityQ[i].priority && ind > -1 && priorityQ[ind].value > priorityQ[i].value)
{
maxpriority = priorityQ[i].priority;
ind = i;
}
else if (maxpriority < priorityQ[i].priority)
{
maxpriority = priorityQ[i].priority;
ind = i;
}
}
// Return position of the element
return ind;
}
int getlowpriority(void)
{
int lowpriority = INT_MAX;
int ind = -1;
// Check for the element with maximum priority
for (int i = 0; i <= lastIndex; i++) {
// Handling edge case of threads with equal priorities : Based on Accept / Decline.
// precedence is given to values of Accept.
/*if (maxpriority == priorityQ[i].priority && ind > -1 && priorityQ[ind].value > priorityQ[i].value)
{
maxpriority = priorityQ[i].priority;
ind = i;
}
else if (maxpriority > priorityQ[i].priority)
{
maxpriority = priorityQ[i].priority;
ind = i;
}*/
//cout << priorityQ[i].priority;
//cout << "\n"<< endl;
if (lowpriority == priorityQ[i].priority && ind > -1 && priorityQ[ind].value > priorityQ[i].value)
{
lowpriority = priorityQ[i].priority;
ind = i;
}
else if (lowpriority > priorityQ[i].priority)
{
lowpriority = priorityQ[i].priority;
ind = i;
}
}
// Return position of the element
return ind;
}
// Function to remove the element with
// the highest priority
void deleteFromQueue(void)
{
// Find the position of the element
// with highest priority
//int ind = getmaxpriority();
int ind = getlowpriority();
// Shift the element one lastIndex before
// from the position of the element
// with highest queueiortity is found
for (int i = ind; i < lastIndex; i++) {
priorityQ[i] = priorityQ[i + 1];
}
// Decrease the lastIndex of the
// priority queue by one
lastIndex--;
}
void loadInPut(void)
{
//You can hard code a few input sets in your code.
insertToQueue(-1,"Acepted");
//insertToQueue(2,"Denied");
//insertToQueue(4,"Denied");
insertToQueue(50,"Accepted"); // creating an edge case of having the same priority
insertToQueue(40,"Accepted");
}
void outFunc1(int ind)
{
cout << priorityQ[ind].priority;
cout << ", Access is granted.\n";
//cout << priorityQ[ind].value << endl;
}
void outFunc2(int ind)
{
cout << priorityQ[ind].priority;
cout << ", Access is denied.\n";
//cout << priorityQ[ind].value << endl;
}
};
int main(int argc, char **argv)
{
// Instantiate an object p of class Priority_Q
Priority_Q p;
// Step 2 : Load the hard coded values.
p.loadInPut();
// We shall use this to loop in the elements.
int li = lastIndex;
/*
* or use string input using getline()
getline(cin,str);
For simplicity, use the hardcoded values.
*
*/
// Stores the top element at the moment
//int ind = p.getmaxpriority();
int ind = p.getlowpriority();
//cout << priorityQ[ind].priority;
//cout << "Initial priority value\n";
while(li>=0) // Loop until you navigate through the entire queue
{
if((priorityQ[ind].value.compare("Accepted")) == 0)
{
p.outFunc1(ind);
}
else if((priorityQ[ind].value.compare("Denied")) == 0)
{
p.outFunc2(ind);
}
else
{
cout << "Illegal queue\n" <<endl;
}
p.deleteFromQueue();
ind = p.getlowpriority();
li--;
}
return 0;
}