-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10866.cpp
97 lines (85 loc) · 1.73 KB
/
10866.cpp
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <stdio.h>
#define DQSIZE 20000
int deque[DQSIZE];
int f = 0;
int r = DQSIZE-1;
int dqsz = 0;
void push_front(int x) {
f = ((f - 1) + DQSIZE) % DQSIZE;
deque[f] = x;
dqsz++;
}
void push_back(int x) {
r = (r + 1) % DQSIZE;
deque[r] = x;
dqsz++;
}
int pop_front() {
if(!dqsz) return -1;
int ret = deque[f];
f = (f + 1) % DQSIZE;
dqsz--;
return ret;
}
int pop_back() {
if(!dqsz) return -1;
int ret = deque[r];
r = ((r - 1) + DQSIZE) % DQSIZE;
dqsz--;
return ret;
}
int size() {
return dqsz;
}
bool empty() {
return dqsz == 0;
}
int front() {
if(empty()) return -1;
return deque[f];
}
int back() {
if(empty()) return -1;
return deque[r];
}
bool str_cmp(char *a, char *b) {
while(*a || *b) {
if(*a++ != *b++) return false;
}
return true;
}
int main() {
int N, x;
char inst[16];
scanf("%d", &N);
while(N--) {
scanf("%s", inst);
if(str_cmp(inst, "push_back")) {
scanf("%d", &x);
push_back(x);
}
else if(str_cmp(inst, "push_front")) {
scanf("%d", &x);
push_front(x);
}
else if(str_cmp(inst, "front")) {
printf("%d\n", front());
}
else if(str_cmp(inst, "back")) {
printf("%d\n", back());
}
else if(str_cmp(inst, "size")) {
printf("%d\n", size());
}
else if(str_cmp(inst, "pop_front")) {
printf("%d\n", pop_front());
}
else if(str_cmp(inst, "pop_back")) {
printf("%d\n", pop_back());
}
else if(str_cmp(inst, "empty")) {
printf("%d\n", empty() ? 1 : 0);
}
}
return 0;
}