Implement a Stack using Queues

Create a Stack data structure designed specifically to store integer data using two queues.

Explanation:

You need to implement a stack using two internal queues. You can utilize any built-in queue functionalities for this purpose.

The goal is to provide implementations for the following public functions:

  1. Constructor: Initialize the queue data members appropriately.
  2. push(data): Insert the integer data into the stack. No return value.
  3. pop(): Remove the integer from the top of the stack and return it. Return -1 if the stack is empty.
  4. top: Fetch the top integer of the stack without removing it. Return -1 if the stack is empty.
  5. size(): Return the current number of elements in the stack.
  6. isEmpty(): Evaluate if the stack is empty and return a boolean value.

Input:

The first line contains an integer 'Q' representing the number of queries. Each of the following 'Q' lines provides an operation to execute on the stack. For a push operation, the input line will include two integers: the type of operation and the integer data being pushed.

Output:

No output for Query-1. For Query-2, output the popped integer. For Query-3, output the top integer. For Query-4, output the size of the stack. For Query-5, output either 'true' or 'false'. Each query result should be on a separate line.

Example:

Input: 6
1 20
1 15
2
3
4
5
Output: 15
20
1
false

Constraints:

  • 1 <= Q <= 1000
  • 1 <= query type <= 5
  • -109 <= data <= 109 and data != -1

Note:

You do not need to explicitly print the result; just implement the solution to manage the internal operations as described.
AnswerBot
10d

Implement a Stack using Queues to store integer data with push, pop, top, size, and isEmpty functions.

  • Use two queues to simulate a stack, with one queue acting as the main stack and the other for temp...read more

Help your peers!
Add answer anonymously...
Samsung Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter