# Sudo Placement[1.3] | Stack Design

Given q number of queries, you need to perform operations on the stack. Queries are of three types 1, 2, and 3. If the operation is to push (1) then push the elements, if the operations are to pop (2) then pop the element and if it is Top (3), then print the element at the top of the stack (If the stack is empty, print “-1”, without quotes).
Examples:

```Input: Queries =  6
3
1 5
1 6
1 7
2
3
Output: -1
6
The first query is to print top, but since the stack is empty, so we print -1.
Next three queries are to push 5, 6, and 7, so we pushed them on a stack.
Next query is pop, so we popped 7 from a stack. Final query is to print the
top, so 6 is there at the top and thus printed.
```

Approach: Stack can be used to perform the given operation. If the input for a query is 1, then take another input and push the element into the stack using the push() function. If the input for a query is 2, then pop the element using the pop() function in a stack. If the input for a query is 3, then print the top element using the top() function.
Below is the implementation of the above approach:

## C++

 `// C++ program for` `// Sudo-Placement | Stack Design` `#include ` `using` `namespace` `std;`   `stack<``int``> s;`   `// function to perform type-1 operation` `void` `_push(``int` `n)` `{` `    ``s.push(n);` `}`   `// function to perform type-2 operation` `void` `_pop()` `{` `    ``s.pop();` `}`   `// function to perform type-3 operation` `void` `print()` `{` `    ``// if the stack is not empty` `    ``if` `(!s.empty())` `        ``cout << s.top() << endl;` `    ``else` `        ``cout << -1 << endl;` `}`   `// Driver Code` `int` `main()` `{` `    ``// 1st query` `    ``print();` `    ``// 2nd query` `    ``_push(5);`   `    ``// 3rd query` `    ``_push(6);`   `    ``// 4th query` `    ``_push(7);`   `    ``// 5th query` `    ``_pop();`   `    ``// 6th query` `    ``print();`   `    ``return` `0;` `}`

## Java

 `import` `java.util.*;` `// Java program for` `// Sudo-Placement | Stack Design`   `class` `GFG ` `{`   `    ``static` `Stack s = ``new` `Stack<>();`   `    ``// function to perform type-1 operation` `    ``static` `void` `_push(``int` `n) ` `    ``{` `        ``s.push(n);` `    ``}`   `    ``// function to perform type-2 operation` `    ``static` `void` `_pop() ` `    ``{` `        ``s.pop();` `    ``}`   `    ``// function to perform type-3 operation` `    ``static` `void` `print() ` `    ``{` `        `  `        ``// if the stack is not empty` `        ``if` `(!s.empty()) ` `        ``{` `            ``System.out.println(s.peek());` `        ``} ` `        ``else` `        ``{` `            ``System.out.println(-``1``);` `        ``}` `    ``}`   `    ``// Driver Code` `    ``public` `static` `void` `main(String[] args) ` `    ``{` `        ``// 1st query` `        ``print();` `        ``// 2nd query` `        ``_push(``5``);`   `        ``// 3rd query` `        ``_push(``6``);`   `        ``// 4th query` `        ``_push(``7``);`   `        ``// 5th query` `        ``_pop();`   `        ``// 6th query` `        ``print();` `    ``}` `}`   `// This code contributed by Rajput-Ji`

## Python3

 `# Python3 program for` `# Sudo-Placement | Stack Design` `s ``=` `[];`   `# function to perform` `# type-1 operation` `def` `_push(n):` `    ``s.append(n);`   `# function to perform ` `# type-2 operation` `def` `_pop():` `    ``s.pop();`   `# function to perform ` `# type-3 operation` `def` `_print():`   `    ``# if the stack is not ` `    ``# empty` `    ``if` `(``len``(s) > ``0``):` `        ``print``(s[``len``(s) ``-` `1``]);` `    ``else``:` `        ``print``(``"-1"``);`   `# Driver Code` `if` `__name__ ``=``=` `'__main__'``:` `  `  `    ``# 1st query` `    ``_print();` `    `  `    ``# 2nd query` `    ``_push(``5``);`   `    ``# 3rd query` `    ``_push(``6``);`   `    ``# 4th query` `    ``_push(``7``);`   `    ``# 5th query` `    ``_pop();`   `    ``# 6th query` `    ``_print();`   `# This code is contributed by 29AjayKumar`

## C#

 `// C# program for` `// Sudo-Placement | Stack Design` `using` `System;` `using` `System.Collections.Generic;`   `class` `GFG ` `{`   `    ``static` `Stack<``int``> s = ``new` `Stack<``int``>();`   `    ``// function to perform type-1 operation` `    ``static` `void` `_push(``int` `n) ` `    ``{` `        ``s.Push(n);` `    ``}`   `    ``// function to perform type-2 operation` `    ``static` `void` `_pop() ` `    ``{` `        ``s.Pop();` `    ``}`   `    ``// function to perform type-3 operation` `    ``static` `void` `print() ` `    ``{` `        `  `        ``// if the stack is not empty` `        ``if` `(s.Count != 0) ` `        ``{` `            ``Console.WriteLine(s.Peek());` `        ``} ` `        ``else` `        ``{` `            ``Console.WriteLine(-1);` `        ``}` `    ``}`   `    ``// Driver Code` `    ``public` `static` `void` `Main() ` `    ``{` `        ``// 1st query` `        ``print();` `        ``// 2nd query` `        ``_push(5);`   `        ``// 3rd query` `        ``_push(6);`   `        ``// 4th query` `        ``_push(7);`   `        ``// 5th query` `        ``_pop();`   `        ``// 6th query` `        ``print();` `    ``}` `}`   `/* This code contributed by PrinciRaj1992 */`

Output:

```-1
6

```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

This article is contributed by Team GeeksforGeeks. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.