-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstack2queue.cpp
More file actions
61 lines (50 loc) · 753 Bytes
/
stack2queue.cpp
File metadata and controls
61 lines (50 loc) · 753 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <stack>
// queue: 1 2
front
// in: 1 2
// out: get front pop in push out get bottom of in
pop:
// in : 1 2
// out: 2 1
class Queue{
stack<int> in;
stack<int> out;
public:
void move()
{
while (!in.empty()){
in x = in.top();
in.pop();
out.push(x);
}
}
void push(int x)
{
in.push(x);
}
void pop() // remove first element in front
{
if (out.empty())
move();
if (!out.empty())
out.pop();
}
int front() // get front element
{
if (out.empty())
move();
if (!out.empty())
return out.top();
}
void empty() // return if queue is empty
{
return in.empty && out.empty;
}
};
int main(){
Queue q;
q.push(10);
q.push(20);
cout << q.front();
return 0;
}