Please enter your email address or userHandle.
This Java module deals with Stacks, Queues, and ArrayLists. We'll perform various operations on them.
Given a stack of integers and Q queries. The task is to perform operation on stack according to the query.
Note: use push() to add element in the stack, peek() to get topmost element without removal, pop() gives topmost element with removal, search() to return position if found else -1.
First line of input contains nubmer of testcases T. For each testcase, first line of input contains Q, number of queries. Next line contains Q queries seperated by space. Queries are as:
i x : (adds element x in the stack).
r : (returns and removes the topmost element from the stack).
h : Prints the topmost element.
f y : (check if the element y is present or not in the stack). Print "Yes" if present, else "No".
For each testcase, perform Q queries and print the output wherever required.
Your task is to complete functions insert(), remove(), headOf_Stack() and find(), to insert, remove returning top element and findiing the elment in stack respectively.
1 <= T <= 100
1 <= Q <= 103
i 2 i 4 i 3 i 5 h f 8
i 3 i 4 r f 3
Testcase 1: Inserting 2, 4, 3, and 5 onto the stack. Returning top element which is 5. Finding 8 will give No, as 8 is not in the stack.
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.