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 QUEUE BY MOVING ELEMENTS ONE DOWN.



#include <iostream>
#include <stdlib.h>
#include <conio.h>

using namespace std;

template <class Type>

class Queue
{
private:
Type *Arr;
int Size, Front;

public:
Queue(int Siz)
{
Size = Siz;

Arr = new Type[Siz];

Front = -1;
}

void EnQueue(Type Ch)
{
if (IsFull())
{
cout << "\nQueue Overflowed!!!\n" << endl;
exit(1);
}

Arr[++Front] = Ch;
}

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

char Ch = Arr[0];

for (int I = 1; I <= Front; I++)
Arr[I -1] = Arr[I];

        Front--;

return Ch;
}

inline bool IsEmpty()
{
return Front == -1;
}

inline bool IsFull()
{
return Front == Size - 1;
}
};

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 QUEUE By Moving One Down IS COMPLEXITY INCREASES WITH THE INCREASE IN SIZE.///
///It can be Improved If we consider our Queue as Circular.///

0 comments:

Post a Comment