Stack Operations
Easy Accuracy: 58.82% Submissions: 10366 Points: 2

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.

Input Format:
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:

  1. i x : (adds element x in the stack).

  2. r : (returns and removes the topmost element from the stack).

  3. h : Prints the topmost element.

  4. f y : (check if the element y is present or not in the stack). Print "Yes" if present, else "No".

Output Format:
For each testcase, perform Q queries and print the output wherever required.

Your Task:
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 are replacing the old Disqus forum with the new Discussions section given below.
Click here to view old Disqus comments.

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?


All Submissions

My Submissions:

Login to access your submissions.

Stack Operations

Output Window