PROGRAM FOR SIMPLEST QUEUE.
#include <iostream>
#include <stdlib.h>
#include <conio.h>
using namespace std;
template <class Type>
class Queue
{
private:
Type *Arr;
int Front, Rear, Size;
public:
Queue(int Siz)
{
Siz = Size;
Arr = new Type[Size];
Front = Rear = -1;
}
void EnQueue(Type Ch)
{
if (IsFull())
{
cout << "\nQueue OverFlowed!!!\n" << endl;
exit(1);
}
Arr[++Rear] = Ch;
}
Type dQueue()
{
if (IsEmpty())
{
cout << "\nQueue UnderFlowed!!!\n" << endl;
exit(1);
}
return Arr[++Front];
}
inline bool IsFull()
{
return Rear == Size - 1;
}
inline bool IsEmpty()
{
return Rear == Front;
}
};
int main()
{
Queue<int> Obj(100);
Obj.EnQueue(1);
Obj.EnQueue(2);
Obj.EnQueue(3);
Obj.EnQueue(4);
Obj.EnQueue(5);
for (int I = 0; I < 5; I++)
cout << Obj.dQueue() << endl;
getch();
return 0;
}
///Drawback Of Simplest Queue is We cannot reuse the Index Of Queue from which value has been Deleted.///
///It Can Be Improved If we Shift All Elements One Down.///
0 comments:
Post a Comment