C++ STL | Set 4 (stack) | Practice | GeeksforGeeks

Implement different operations on a stack s .

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 to the stack s )
2. b (if stack is not empty pops top element and prints it, else prints -1)
3. c (prints the size of the stack )
4. d (if stack is not empty prints the top element of the stack, 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 
3
a 55 a 11 d 
 
Output
7 2
11

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

For the sec test case 
There are three queries. Queries are performed in this order
1. a 55  (stack s has 55}
2. a 11  (stack s has 55 ,11}
3. d      (prints the top element of the stack s 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.
0
nishasingh005063 months ago
void push(stack<int> &s,int x){ s.push(x);}
/*pops the top element of thestack and returns it */int pop(stack<int> &s){     while(!s.empty())   { int item= s.top();  s.pop();   return item;   } return -1;}
/*returns the size of the stack */int getSize(stack<int> &s){  int size= s.size();  return size;}
/*returns the top element of the stack */int getTop(stack<int> &s){      while(!s.empty())   { int t= s.top(); return t;   }   return -1;}
0
kumarnitesh884414 months ago
void push(stack<int> &s,int x){   //Your code here      s.push(x);}
/*pops the top element of thestack and returns it */int pop(stack<int> &s){   //Your code here   if(s.size()>0){       int res=s.top();       s.pop();       return res;   }   return -1;}
/*returns the size of the stack */int getSize(stack<int> &s){  //Your code here  return s.size();}
/*returns the top element of the stack */int getTop(stack<int> &s){  //Your code here if(s.size()>0){     return s.top(); } return -1;}
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.