Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 5761 |

@blackshadows | 5715 |

@akhayrutdinov | 5111 |

@mb1973 | 4989 |

@Quandray | 4944 |

@saiujwal13083 | 4506 |

@sanjay05 | 3762 |

@marius_valentin_dragoi | 3516 |

@sushant_a | 3459 |

@verma_ji | 3341 |

@KshamaGupta | 3318 |

Complete Leaderboard | |

Handle | Score |

@aroranayan999 | 1115 |

@bt8816103042 | 739 |

@SherlockHolmes3 | 447 |

@codeantik | 441 |

@SHOAIBVIJAPURE | 430 |

@shalinibhataniya1097 | 408 |

@ShamaKhan1 | 392 |

@neverevergiveup | 381 |

@amrutakashikar2 | 355 |

@mahlawatep | 353 |

@murarry3625 | 352 |

Complete Leaderboard |

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

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}

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 )

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes
C++ STL | Set 4 (stack)

...