If Any Required Program Please Ask In Comment I Will Help You(any Program in JAVA or C++) . . THANKS FOR VISITING MY BLOG!

If U LIKE MY PROFILE RAISE YOUR HAND IF U NOT RAISE UR STANDARD. Powered by Blogger.

Sunday, October 21, 2018

PROGRAM FOR CIRCULAR 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)
{
Size = Siz;

Arr = new Type[Size];

Front = Rear = Size - 1;
}

void EnQueue(Type Ch)
{
int Temp = (Rear + 1) % Size;
if (Temp == Front)
{
cout << "\nQueue OverFlowed..!!!\n" << endl;
exit(1);
}

Rear = (Rear + 1) % Size;
Arr[Rear] = Ch;
}

Type dQueue()
{
if (IsEmpty())
{
cout << "\nStack UnderFlowed..!!!\n" << endl;
exit(1);
}

Front = (Front + 1) % Size;
return Arr[Front];
}

inline bool IsEmpty()
{
return Front == Rear;
}
};

int main()
{
Queue<int> Obj(10);

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;
}

0 comments:

Post a Comment