-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxInStack.cpp
More file actions
44 lines (34 loc) · 826 Bytes
/
MaxInStack.cpp
File metadata and controls
44 lines (34 loc) · 826 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// MaxInStack.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <stack>
using namespace std;
int maxElement(stack<int> &targetStack) {
stack<int> auxStack;
int max = 0;
while (!targetStack.empty()) {
if (max < targetStack.top())max = targetStack.top();
auxStack.push(targetStack.top());
targetStack.pop();
}
while (!auxStack.empty()) {
targetStack.push(auxStack.top());
auxStack.pop();
}
return max;
}
int main(){
int input, numberOfQueries;
stack<int> mainStack;
cin >> numberOfQueries;
for (int i = 0; i < numberOfQueries; i++) {
cin >> input;
switch (input) {
case 1: cin >> input; mainStack.push(input); break;
case 2: mainStack.pop(); break;
case 3: cout << maxElement(mainStack) << endl; break;
}
}
return 0;
}