-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path18258_queue_implement.cpp
More file actions
65 lines (59 loc) · 1.23 KB
/
18258_queue_implement.cpp
File metadata and controls
65 lines (59 loc) · 1.23 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
#include <iostream>
#define MAX 2000000
using namespace std;
class Queue
{
private:
int f = 0;
int b = -1;
int arr[MAX];
public:
void push(int x);
void pop();
void size();
void empty();
void front();
void back();
};
void Queue::push(int x) { arr[++b] = x; }
void Queue::pop() {
if (f <= b) cout << arr[f++] << '\n';
else cout << -1 << '\n';
}
void Queue::size() { cout << b - f + 1 << '\n'; }
void Queue::empty() {
if (f > b) cout << 1 << '\n';
else cout << 0 << '\n';
}
void Queue::front() {
if (f <= b) cout << arr[f] << '\n';
else cout << -1 << '\n';
}
void Queue::back() {
if (f <= b) cout << arr[b] << '\n';
else cout << -1 << '\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
Queue q;
string str;
int n;
cin >> n;
while(n--) {
cin >> str;
if (str == "push") {
int a;
cin >> a;
q.push(a);
}
else if (str == "pop") { q.pop(); }
else if (str == "size") { q.size(); }
else if (str == "empty") { q.empty(); }
else if (str == "front") { q.front(); }
else if (str == "back") { q.back(); }
}
return 0;
}