C++ STL | Set 5 (queue) | Practice | GeeksforGeeks

Implement different operations on a queue q .

Input:
The first line of input contains an integer T denoting the no of test cases . Then T test cases follow. The first line of input contains an integer Q denoting the no of queries . Then in the next line are Q space separated queries .
A query can be of four types 
1. a x (Pushes an element x at the end of the queue q )
2. b (if queue is not empty pops the front element and prints it, else prints -1)
3. c (prints the size of the queue)
4. d (if queue is not empty prints the front element of the queue, else prints -1)
5. e (if queue is not empty prints the last element of the queue else prints -1)

Output:
The output for each test case will  be space separated integers denoting the results of each query . 

Constraints:
1<=T<=100
1<=Q<=100

Example:
Input
2
5
a 4 a 6 a 7 b c 
4
a 55 a 11 d e
 
Output
4 2
55 11

Explanation :
For the first test case
There are five queries. Queries are performed in this order
1. a 4 { queue q has 4  }
2. a 7 {queue q has 4,7 }
3. a 6 {queue q has 4,7,6}
4. b    {pop 4 from queue q and prints it queue now has 7,6}
5. c    {prints the size of the queue q ie 2}

For the sec test case 
There are three queries. Queries are performed in this order
1. a 55  {queue q has 55 }
2. a 11  {queue q has 55 ,11 }
3. d      {prints the front element of the queue q ie. 55 }
4. e      {prints the end element of the queue q ie 11 }



Note:The Input/Output format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
+1
aqquilin1413 months ago
void push(queue<int> &q,int x){
 //Your code here
    q.push(x);
}
int pop(queue<int> &q)
{
   //Your code here
   if(!q.empty()){
       int x = q.front();
       q.pop();
       return x;
   }
   return -1;
}
int getSize(queue<int> &q)
{
    //Your code here
    return q.size();
}
int getBack(queue<int> &q)
{
//Your code here
if(!q.empty()){
return q.back();}
return -1;
}

int getFront(queue<int> &q)
{
//Your code here
 if(!q.empty()){
return q.front();}
return -1;
}
 
+1
kumarnitesh884414 months ago
void push(queue<int> &q,int x){   //Your code here   q.push(x);}
/*pop out the front element from the queue q and returns it */int pop(queue<int> &q){  //Your code here  if(q.size()>0){      int res=q.front();      q.pop();      return res;  }  return -1;}
/*returns the size of the queue q */int getSize(queue<int> &q){   //Your code here   return q.size();}
/*returns the last element of the queue */int getBack(queue<int> &q){//Your code hereif(q.size()>0){    int r=q.back();    return r;}return -1;}
/*returns the first element of the queue */int getFront(queue<int> &q){//Your code hereif(q.size()>0){    int s=q.front();    return s;    }return -1;}
-1
Siddharth Katiyar1 year ago
Siddharth Katiyar
void push(queue<int> &q,int x){    q.push(x);    //Your code here}/*pop out the front element from the queue q and returns it */int pop(queue<int> &q){    if(!q.empty()){        int front = q.front();         q.pop();         return front;    }     return -1;   //Your code here}/*returns the size of the queue q */int getSize(queue<int> &q){    return q.size();    //Your code here}/*returns the last element of the queue */int getBack(queue<int> &q){    if(!q.empty()){        int r = q.back();           return r;    }     return -1;//Your code here}/*returns the first element of the queue */int getFront(queue<int> &q){    if(!q.empty()){        int f = q.front();           return f;    }     return -1;//Your code here}
+1
Rishabh Patel1 year ago
Rishabh Patel
0
Rishabh Patel
This comment was deleted.
0
Debojyoti Sinha1 year ago
Debojyoti Sinha
Correct Answer.Correct AnswerExecution Time:0.01
void push(queue<int> &q, int x){    q.push(x);}int pop(queue<int> &q){    if(q.empty() == true)    {        return -1;    }        int val = q.front();        q.pop();        return val;}int getSize(queue<int> &q){    return q.size();}int getBack(queue<int> &q){    if(q.empty() == true)    {        return -1;    }        return q.back();}int getFront(queue<int> &q){    if(q.empty() == true)    {        return -1;    }        return q.front();}
0
Arijit Biswas2 years ago
Arijit Biswas
void push(queue<int> &q,int x){    q.push(x);}int pop(queue<int> &q){   if (!q.empty())    {        int x= q.front();        q.pop();        return x;    }    else        return -1;}int getSize(queue<int> &q){    return q.size();}int getBack(queue<int> &q){return q.empty() ? -1 : q.back();}int getFront(queue<int> &q){return q.empty() ? -1 : q.front();}
0
NITIN SHARMA2 years ago
NITIN SHARMA
https://ide.geeksforgeeks.o...Correct Answer.C++Execution Time:0.01
0
Arun Karthik2 years ago
Arun Karthik
Correct Answer.Correct AnswerExecution Time:0.01
https://ide.geeksforgeeks.o...
void push(queue<int> &q,int x){    q.push(x);}
int pop(queue<int> &q){   if(q.empty()) return -1;   int x=q.front();   q.pop();   return x;}
int getSize(queue<int> &q){   return q.size();}
int getBack(queue<int> &q){if(q.empty()) return -1;return q.back();}
int getFront(queue<int> &q){if(q.empty()) return -1;return q.front();}
0
Yash Parmar2 years ago
Yash Parmar
void push(queue<int> &q,int x){    //Your code here    q.push(x);}
/*pop out the front element from the queue q and returns it */int pop(queue<int> &q){   //Your code here   if(q.empty())   return -1;   int x = q.front();   q.pop();   return x;}
/*returns the size of the queue q */int getSize(queue<int> &q){    //Your code here    return q.size();}
/*returns the last element of the queue */int getBack(queue<int> &q){//Your code hereif(q.empty())return -1;return q.back();}
/*returns the first element of the queue */int getFront(queue<int> &q){//Your code hereif(q.empty())return -1;return q.front();}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.