-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue_stack.cpp
More file actions
45 lines (40 loc) · 854 Bytes
/
Copy pathqueue_stack.cpp
File metadata and controls
45 lines (40 loc) · 854 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
44
45
#include <stdio.h>
#include <limits.h>
#include <stack>
class Queue {
std::stack<int> stackOld, stackNew;
int popTop(std::stack<int> &tstack) {
int ret = tstack.top();
tstack.pop();
return ret;
}
public:
static const int ERROR = INT_MIN;
void push(int val) {
stackNew.push(val);
}
int pop() {
if(!stackOld.empty())
return popTop(stackOld);
while(!stackNew.empty())
stackOld.push(popTop(stackNew));
if(!stackOld.empty())
return popTop(stackOld);
return ERROR;
}
bool empty() { return stackOld.empty() && stackNew.empty(); }
};
int main() {
Queue myQueue;
myQueue.push(1);
myQueue.push(2);
int val = myQueue.pop();
printf("1 = %d\n", val);
myQueue.push(3);
val = myQueue.pop();
printf("2 = %d\n", val);
val = myQueue.pop();
printf("3 = %d\n", val);
val = myQueue.pop();
printf("INT_MIN = %d\n", val);
}