-
Notifications
You must be signed in to change notification settings - Fork 111
/
solution.cpp
85 lines (76 loc) · 1.57 KB
/
solution.cpp
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <climits>
#include <deque>
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
int readline(string str) {
int i = 0;
char ch;
while(ch!= '\n') {
str[i++] = ch;
}
str[i] = '\0';
return i;
}
stack<int> s1;
stack<int> s2;
int front_val;
//to insert element to queue
void enqueue(int x) {
while(!s2.empty()) {
//push the element on top of s2 to s1 and pop it from s2
s1.push(s2.top());
s2.pop();
}
//the element to be enqueued will be the front value if s1 is empty
if(s1.empty()) {
front_val = x;
}
//push the element to s1
s1.push(x);
}
//to delete element from queue
int dequeue() {
while(!s1.empty()) {
//push the top element of s1 to s2 and pop it from s1
s2.push(s1.top());
s1.pop();
}
//store the top of s2 into variable which is the dequed element
int x = s2.top();
s2.pop();
if(!s2.empty()) {
front_val = s2.top();
}
return x;
}
//to get the front value
int front() {
return front_val;
}
int main(int argc, char *argv[]) {
int t;
scanf("%d", &t);
while(t--) {
int op;
scanf("%d", &op);
switch(op) {
case 1:
int x;
scanf("%d", &x);
enqueue(x);
break;
case 2:
dequeue();
break;
case 3:
printf("%d\n", front());
break;
}
}
return 0;
}