-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
65 lines (50 loc) · 1.18 KB
/
main.cpp
File metadata and controls
65 lines (50 loc) · 1.18 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
53
54
55
56
57
58
59
60
61
62
63
64
65
//implementing a queue using 2 stacks
#include <stack>
#include <iostream>
using namespace std;
class MyQueue{
private:
stack<int> wrong; //enqueue
stack<int> right; //dequeue
public:
MyQueue(){}
void push(int x){
wrong.push(x);
}
int pop(){
int front;
if(right.empty()){
while(!wrong.empty()){
right.push(wrong.top());
wrong.pop();
}
}
front = right.top();
right.pop();
return front;
}
int peek(){
if(right.empty()){
while(!wrong.empty()){
right.push(wrong.top());
wrong.pop();
}
}
return right.top();
}
bool empty(){
if(right.empty() && wrong.empty()){
return true;
}else{
return false;
}
}
};
int main(){
MyQueue* myQueue = new MyQueue();
myQueue->push(1); // queue is: [1]
myQueue->push(2); // queue is: [1, 2] (leftmost is front of the queue)
cout << myQueue->peek() << endl; // return 1
cout << myQueue->pop() << endl; // return 1, queue is [2]
myQueue->empty(); // return false
}