PRIORITY QUEUE IN ASCENDING ORDER USING SORTING TECHNIQUES.
#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[Size];
Rear = -1;
}
void EnQueue(Type Ch)
{
if (IsFull())
{
cout << "\nQueue OverFlowed...!!!\n" << endl;
exit(1);
}
Queue[++Rear] = Ch;
}
Type dQueue()
{
if (IsEmpty())
{
cout << "\nQueue Underflowed...!!!\n" << endl;
exit(1);
}
Bubble_Sort(Queue);
///Select_Sort(Queue);
Type Value = Queue[0];
for (int I = 0; I < Rear; I++)
Queue[I] = Queue[I + 1];
Rear--;
return Value;
}
void Select_Sort(Type *Arr)
{
int Min, Temp;
for (int I = 0; I <= Rear - 1; I++)
{
for (int J = I + 1; J <= Rear; J++)
{
if (Arr[J] < Arr[I])
{
Min = J;
Temp = Arr[I];
Arr[I] = Arr[Min];
Arr[Min] = Temp;
}
}
}
}
void Bubble_Sort(Type *Arr)
{
int Temp;
for (int I = 0; I < Rear - 1; I++)
{
for (int J = 0; J <= Rear - I - 1; J++)
{
if (Arr[J] > Arr[J + 1])
{
Temp = Arr[J];
Arr[J] = Arr[J + 1];
Arr[J + 1] = Temp;
}
}
}
}
inline bool IsEmpty()
{
return (Rear == -1);
}
inline bool IsFull()
{
return (Rear == Size - 1);
}
};
int main()
{
PQueue<int> Obj(10);
Obj.EnQueue(4);
Obj.EnQueue(5);
Obj.EnQueue(1);
Obj.EnQueue(3);
Obj.EnQueue(2);
for (int I = 0; I < 5; I++)
cout << Obj.dQueue() << endl;
getch();
return 0;
}
0 comments:
Post a Comment