-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.cpp
More file actions
52 lines (47 loc) · 1.05 KB
/
program.cpp
File metadata and controls
52 lines (47 loc) · 1.05 KB
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
#include "../include/pre.h"
#include <stack>
class Queue {
private:
std::stack<int> cache_in;
std::stack<int> cache_out;
void move_to_out_cache() //require cache_out to be empty
{
while(!cache_in.empty()) {
cache_out.push(cache_in.top());
cache_in.pop();
}
}
public:
// Push element x to the back of queue.
void push(int x) {
cache_in.push(x);
}
// Removes the element from in front of queue.
void pop(void) {
if (cache_out.empty()) move_to_out_cache();
cache_out.pop();
}
// Get the front element.
int peek(void) {
if (cache_out.empty()) move_to_out_cache();
return cache_out.top();
}
// Return whether the queue is empty.
bool empty(void) {
return cache_in.empty() && cache_out.empty();
}
};
int main()
{
Queue q;
q.push(1);
q.push(2);
q.push(3);
q.push(4);
cout << q.peek() << endl;
q.pop();
cout << q.peek() << endl;
q.pop();
cout << q.peek() << endl;
return 0;
}