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 DeQUEUE.



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

using namespace std;

template <class Type>

class DeQueue
{
private:
Type *Queue;
int Size, Front, Rear, Count;

public:
DeQueue(int Siz)
{
Size = Siz;
Queue = new Type[Size];

Front = Count = -1;
Rear = Size - 1;
}

void EnQueue_Beg(Type Ch)
{
if (IsFull())
{
cout << "\nSTACK OVERFLOWED...!!!\n" << endl;
exit(1);
}

Count++;
Queue[++Front] = Ch;
}

void EnQueue_End(Type Ch)
{
if (IsFull())
{
cout << "\nSTACK OVERFLOWED...!!!\n" << endl;
exit(1);
}

Count++;
Queue[Rear--] = Ch;
}

Type dQueue_Beg()
{
if (IsEmpty())
{
cout << "\nSTACK UNDERFLOWED...!!!\n" << endl;
exit(1);
}

Count--;
return Queue[Front--];
}

Type dQueue_End()
{
if (IsEmpty())
{
cout << "\nSTACK UNDERFLOWED...!!!\n" << endl;
exit(1);
}

Count--;
return Queue[++Rear];
}

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

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

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

Obj.EnQueue_Beg(1);
Obj.EnQueue_End(99);

cout << "Element From Beginning: " << Obj.dQueue_Beg() << endl;
cout << "Element From End: " << Obj.dQueue_End() << endl;

getch();
return 0;
}

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

PRIORITY QUEUE IN DESCENDING 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 Max, Temp;

for (int I = 0; I <= Rear - 1; I++)
{

for (int J = I + 1; J <= Rear; J++)
{
if (Arr[J] > Arr[I])
{
Max = J;

Temp = Arr[I];
Arr[I] = Arr[Max];
Arr[Max] = 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;
}

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

PROGRAM FOR PRIORITY QUEUE FOR DESCENDING 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;
}

Type dQueue()
{
int Index = Maxi();
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--;
return Value;
}

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

int Maxi()
{
int Index;
Type Big = Queue[0];

for (int I = 1; I <= Rear; I++)
{
if (Big < Queue[I])
{
Index = I;
Big = 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++)
cout << Obj.dQueue() << endl;

getch();
return 0;
}

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

CONVERTING INFIX EXPRESSION INTO PREFIX EXPRESSION.



#include <bits/stdc++.h>
#include <iostream>
#include <string.h>
#include <cctype>
#include <stdlib.h>
#include <conio.h>

using namespace std;

template <class Type>

class Infix2Prefix
{
private:
    Type *Stack;
    int Size, Top;

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

        Stack = new Type[Size];
        strcpy(Stack, "\0");

        Top = -1;
    }

    void Push(Type Ch)
    {
        if( IsFull() )
        {
            cout << "\nSTACK OVERFLOWED!!!!!!!!!\n" << endl;
            exit(1);
        }

        Stack[++Top] = Ch;
    }

    Type Pop()
    {
        if( IsEmpty() )
        {
            cout << "\nSTACK UNDERFLOWED!!!!!!\n" << endl;
            exit(1);
        }

        return Stack[Top--];
    }

    Type TOP()
    {
        return Stack[Top];
    }

    bool IsOperator(char Ch)
    {
        return ( !isalpha(Ch) && !isdigit(Ch) );
    }

    int Priority(char Ch)
    {
        if(Ch == '^')
            return 3;

       else if( Ch == '*' || Ch == '/' )
            return 2;

        else if(Ch == '+' || Ch == '-')
            return  1;

            return 0;
    }

    string Convert(string Infix)
    {
        char Ch;
        string Prefix;

        reverse( Infix.begin(), Infix.end() );

        for(int I =0; I <Infix.length(); I++)
        {
            if( Infix[I] == '(' )
                Infix[I++] = ')';

            else if( Infix[I] == ')' )
                Infix[I++] = '(';
        }

        for(int I =0; I <Infix.length(); I++)
        {
            Ch = Infix[I];

            if( isalpha(Ch) || isdigit(Ch) )
                Prefix += Ch;

            else if( Ch == '(' )
                    Push(Ch);

            else if( Ch == ')' )
            {
                for(; TOP() !='(';)
                {
                    Prefix += Pop();
                }

                Pop();
            }

            else
            {
                if(IsOperator( TOP() ) )
                   {
                       while( Priority(Ch) <= Priority( TOP() ) )
                             {
                                 Prefix += Pop();
                             }

                             Push(Ch);
                    }
            }
}

Prefix += Pop();

reverse(Prefix.begin(), Prefix.end());

        return Prefix;
    }

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

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

    ~Infix2Prefix()
    {
        cout << "\n\n\n****************PROGRAM ENDED*******************\n\n\n" << endl;
    }
};

int main()
{
    Infix2Prefix<char> Obj(100);
    string Expression;

    cout << "Enter any INFIX Expression: ";
    cin >> Expression;

    cout << "\nExpression After Converting To PRETFIX is: " << Obj.Convert(Expression) << endl;

    getch();
    return 0;
}