Skip to content

Commit 99f5fab

Browse files
authored
Priority Queue implementation in C++ (#77)
1 parent dc19003 commit 99f5fab

File tree

1 file changed

+171
-0
lines changed

1 file changed

+171
-0
lines changed

priorityQueue.cpp

+171
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
#include <iostream>
2+
#include <stdio.h>
3+
#include <cstring>
4+
using namespace std;
5+
struct data
6+
{
7+
char job[10];
8+
int prno , ord;
9+
10+
};
11+
const int MAX=5;
12+
class priority_queue
13+
{
14+
private:
15+
data d[MAX];
16+
int front,rear;
17+
public:
18+
priority_queue()
19+
{
20+
front=rear=-1;
21+
for(int i=0;i<MAX;i++)
22+
{
23+
strcpy(d[i].job,"\0");
24+
d[i].prno=d[i].ord=0;
25+
}
26+
}
27+
void add(data dt)
28+
{
29+
if(rear==MAX-1)
30+
{
31+
cout<<"Queue is full";
32+
}
33+
else
34+
{
35+
rear++;
36+
d[rear]=dt;
37+
if(front==-1)
38+
{ front=0; }
39+
40+
}
41+
42+
}
43+
void remove()
44+
{
45+
int highestPriority = 0;
46+
int ind = -1;
47+
data temp;
48+
for(int i=front;i<=rear;i++)
49+
{
50+
if (highestPriority == d[i].prno && ind > -1 && d[ind].job < d[i].job)
51+
{
52+
highestPriority = d[i].prno;
53+
ind = i;
54+
}
55+
else if (highestPriority < d[i].prno)
56+
{
57+
highestPriority = d[i].prno;
58+
ind = i;
59+
}
60+
}
61+
62+
if(front==-1)
63+
{
64+
cout<<"Queue is empty";
65+
66+
}
67+
68+
else
69+
{
70+
for(int i=ind;i<rear;i++)
71+
{
72+
d[i] = d[i + 1];
73+
74+
}
75+
76+
}
77+
rear--;
78+
}
79+
80+
void sort()
81+
{
82+
data temp;
83+
for(int i=front;i<rear;i++)
84+
{
85+
for(int j=i+1; j<=rear;j++)
86+
{
87+
if(d[i].prno>d[j].prno)
88+
{
89+
temp=d[i];
90+
d[i]=d[j];
91+
d[j]=temp;
92+
}
93+
else if(d[i].prno==d[j].prno)
94+
{
95+
if(d[i].ord>d[j].ord)
96+
{
97+
temp=d[i];
98+
d[i]=d[j];
99+
d[j]=temp;
100+
}
101+
}
102+
}
103+
}
104+
display();
105+
}
106+
107+
void display()
108+
{
109+
if(front==-1)
110+
{
111+
cout<<"Queue is empty";
112+
}
113+
else
114+
{
115+
for(int i=front;i<=rear;i++)
116+
{
117+
cout<<d[i].job<<" ";
118+
}
119+
}
120+
cout<<endl;
121+
}
122+
};
123+
int main ()
124+
{
125+
priority_queue p;
126+
int ord=0;
127+
while(true)
128+
{
129+
cout<< "Priority Queue Operations :- "<<endl;
130+
cout<< "1 -> ADD"<<endl;//add element at end
131+
cout<< "2 -> REMOVE "<<endl; //removes element from front
132+
cout<< "3 -> DISPLAY"<<endl;//display all the elements
133+
cout<< "4 -> SORT BY PRIORITY "<<endl;
134+
cout<< "5 -> EXIT"<<endl;
135+
int c;
136+
cout<<"Enter your choice :";
137+
cin >>c;
138+
switch(c)
139+
{
140+
case 1:
141+
data d;
142+
cout<<"Enter the data : ";
143+
cin >>d.job;
144+
cout<<"Enter the Priority no : ";
145+
cin >>d.prno;
146+
d.ord=ord++;
147+
p.add(d);
148+
break;
149+
case 2:
150+
p.remove();
151+
break;
152+
case 3:
153+
p.display();
154+
break;
155+
case 4:
156+
p.sort();
157+
break;
158+
case 5:
159+
exit(0);
160+
default:
161+
cout<<"Invalid Input"<<endl;
162+
false;
163+
break;
164+
165+
166+
}
167+
}
168+
169+
170+
return 0 ;
171+
}

0 commit comments

Comments
 (0)