+92 332 4229 857 99ProjectIdeas@Gmail.com

How to use Queue class (C++)


Queue
Queue is FIFO (First in First out) data structure in which elements are inserted at one end and extracted from the other end. Basic functions of the Queue class are as:
push() function pushes the element in the Queue.
pop() function pops the element from the Queue.
size() returns the size of the Queue.
empty() returns 1 if Queue is empty and 0 in case if Queue is not empty.
front() returns the first element of the queue.
back() returns the last element of the queue.

Following code demonstrates the use of the Queue class.

Code

#include "stdafx.h"
#include "iostream"
#include "conio.h"
#include "queue"

using namespace std;

int main()
{
    queue<int> Q;
    Q.push(1);
    Q.push(2);
    Q.push(3);
          
              cout<<"Size     > "<<Q.size()<<endl;
              cout<<"Is Empty > "<<Q.empty()<<endl;
             
              cout<<"Elements poping !!! "<<endl;
              while(!Q.empty())
              {
                     cout<<Q.front()<<endl;
                     Q.pop();
              }
              cout<<"Elements poped  !!! "<<endl;

              cout<<"Size     > "<<Q.size()<<endl;
              cout<<"Is Empty > "<<Q.empty()<<endl;

    _getche();
    return 0;
}
Output
Size     > 3
Is Empty > 0
Elements poping !!!
1
2
3
Elements poped  !!!
Size     > 0
Is Empty > 1

0 comments: