-
Notifications
You must be signed in to change notification settings - Fork 0
/
10845_큐_구현.cpp
91 lines (78 loc) · 1.48 KB
/
10845_큐_구현.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
#include <iostream>
#include <string>
using namespace std;
struct queue {
int queue_data[10000]; // 명령의 수가 10000으로 주어져 있으므로 10000으로 설정해 둔다.
int begin;
int end;
queue() {
begin = 0;
end = 0;
}
void push(int num) {
queue_data[end] = num;
end++;
}
int pop() {
if (empty()) {
return -1;
}
return queue_data[begin++];
}
int size() {
return end - begin;
}
int empty() {
if (size() == 0) {
return 1;
}
return 0;
}
int front() {
if (empty()) {
return -1;
}
return queue_data[begin];
}
int back() {
if (empty()) {
return -1;
}
return queue_data[end - 1];
}
};
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
queue q;
int num_of_commands;
string current_command; //입력받을 명령
int value; //입력받을 값
cin >> num_of_commands;
// 명령의 갯수를 입력받고 for문에서 해당 갯수만큼 돌린다.
for (int i = 0; i < num_of_commands; i++)
{
cin >> current_command;
if (current_command == "push") {
cin >> value;
q.push(value);
}
else if (current_command == "pop") {
cout << q.pop() << "\n";
}
else if (current_command == "size") {
cout << q.size() << "\n";
}
else if (current_command == "empty") {
cout << q.empty() << "\n";
}
else if (current_command == "front") {
cout << q.front() << "\n";
}
else if (current_command == "back") {
cout << q.back() << "\n";
}
}
return 0;
}