42.(C++/DS) _----_>>>> Implementation of Queue data structure with array .

 #include<iostream>

using namespace std;

#define p 100000

struct Queue{

  int q[p],front,rear;

  Queue();//constructor

  void push(int j);

  int pop();

  int Front();

  bool empty();





};

Queue::Queue(){

       front=rear=-1;// will initialize f,r with -1;


}

void Queue::push(int x) // will help enter any value into queue.

{

    if(rear+1==p) // rear+1 means the value is no out of our queue size(p-1);

    {

        cout<<"Sorry,sir.Your Queue is Overflowing\n";

        return ;


    }


    if(front==-1 && rear==-1) // it initial moment we have to increase front just for one time but the

                                // rear will go automatically.

        front++;

     q[++rear]=x;


}

int Queue::pop() // it will delete the value which was entered  last.

{

    if(empty())

    {

         cout<<"Sorry,sir.Your Queue is Underflowing\n";


    }

     int x = q[front++]; // first it will return the value then it will point the next value.


     if(rear == front-1) front = rear = -1;


     return x;




}

int Queue::Front() // will help us to see our last entered value.

{

    if(empty())

    {

        cout<<"NO front to show\n";

    }

    return q[front];

}

bool Queue::empty() // will check if out queue is empty or not.

{

    return !~front; // if the front is -1 then it means queque is empty // ~-1=0 ->> !0=1.

}


int main()

{

    Queue q;

    q.push(54);

    q.push(85);

    cout << q.Front() << "\n";

    cout << q.pop() << "\n";

    cout << q.empty() << "\n";

    cout << q.Front() << "\n";

    cout << q.pop() << "\n";

    cout << q.empty() << "\n";

    cout << q.pop() << "\n";

    cout << q.empty() << "\n";

    q.push(96);

    cout << q.empty() << "\n";

    cout << q.Front() << "\n";

    cout << q.pop() << "\n";

    cout << q.empty() << "\n";

    cout << q.Front() << "\n";








    return 0;

}


Comments

Popular Posts