PROGRAM FOR PRIORITY QUEUE FOR ASCENDING ORDER.
#include <iostream>
#include <stdlib.h>
#include <conio.h>
using namespace std;
template <class Type>
class PQueue
{
private:
Type *Queue;
int Size, Rear;
public:
PQueue(int Siz)
{
Size = Siz;
Queue = new Type[Siz];
Rear = -1;
}
void EnQueue(Type Ch)
{
if (Rear == Size - 1)
{
cout << "\nQueue OverFlowed!!!\n" << endl;
exit(1);
}
Queue[++Rear] = Ch;
}
void dQueue()
{
int Index = Mini();
Type Value = Queue[Index];
if (IsEmpty())
{
cout << "\nQueue UnderFlowed!!!\n" << endl;
exit(1);
}
for (int I = Index; I < Rear; I++)
Queue[I] = Queue[I + 1];
Rear--;
cout<<Value<<endl;
}
inline bool IsEmpty()
{
return (Rear == -1);
}
int Mini()
{
int Index;
Type Small = Queue[0];
for (int I = 1; I <= Rear; I++)
{
if (Small > Queue[I])
{
Index = I;
Small = Queue[I];
}
}
return Index;
}
};
int main()
{
PQueue<int> Obj(10);
Obj.EnQueue(3);
Obj.EnQueue(5);
Obj.EnQueue(7);
Obj.EnQueue(8);
Obj.EnQueue(1);
for (int I = 0; I < 5; I++)
Obj.dQueue() << endl;
getch();
return 0;
}
0 comments:
Post a Comment