-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathback_10866.cpp
More file actions
92 lines (76 loc) · 1.66 KB
/
back_10866.cpp
File metadata and controls
92 lines (76 loc) · 1.66 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
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
#include <iostream>
#include <string>
using namespace std;
const int MX = 1000005;
int dat[2 * MX + 1];
int head = MX, tail = MX;
int empty(int dat[]) {
if (head == tail)
return 1;
else return 0;
}
void push_front(int dat[], int data) {
dat[--head] = data;
}
void push_back(int dat[], int data) {
dat[tail++] = data;
}
int pop_front(int dat[]) {
if (empty(dat))
return -1;
return dat[head++];
}
int pop_back(int dat[]) {
if (empty(dat))
return -1;
return dat[--tail];
}
int size(int dat[]) {
return tail - head;
}
int front(int dat[]) {
if (empty(dat))
return - 1;
return dat[head];
}
int back(int dat[]) {
if (empty(dat))
return -1;
return dat[tail - 1];
}
int main() {
int num;
cin >> num;
for (int i = 0; i < num; i++) {
string com;
cin >> com;
if (com == "push_front") {
int com_end;
cin >> com_end;
push_front(dat, com_end);
}
else if (com == "push_back") {
int com_end;
cin >> com_end;
push_back(dat, com_end);
}
else if (com == "pop_front") {
cout << pop_front(dat) << '\n';
}
else if (com == "pop_back") {
cout << pop_back(dat) << '\n';
}
else if (com == "size") {
cout << size(dat) << '\n';
}
else if (com == "empty") {
cout << empty(dat) << '\n';
}
else if (com == "front") {
cout << front(dat) << '\n';
}
else if (com == "back") {
cout << back(dat) << '\n';
}
}
}