Baekjoon 18258 (큐 2)

Baekjoon 18258 (큐 2)

Baekjoon 18258, 백준 18258 문제의 본인 풀이입니다!
문제는 아래의 링크에서 확인할 수 있습니다.
문제보기

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
#include <iostream>
#include <string>
using namespace std;
#define size 20000001

int* arr = new int[size];
int last = 0;
int start = 0;

void switch_string(string s){
if(s == "push"){
int num;
cin >> num;
arr[last] = num;
last++;
}

else if(s == "pop") {
if(last - start == 0) cout << -1 << '\n';
else {
cout << arr[start] << '\n';
start++;
}
}

else if(s == "size"){
cout << last - start << '\n';
}

else if(s == "empty"){
if(last - start == 0) cout << 1 << '\n';
else cout << 0 << '\n';
}

else if(s == "front"){
if(last - start == 0) cout << -1 << '\n';
else cout << arr[start] << '\n';
}

else if(s == "back"){
if(last - start == 0) cout << -1 << '\n';
else cout << arr[last - 1] << '\n';
}
}

int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);

int N;
cin >> N;
while(N--){
string s;
cin >> s;
switch_string(s);
}

delete[] arr;
}

두 번째 스터디가 어느 새 첫 번째 스터디의 진도를 따라잡았다..
저번의 다짐대로 큐, 덱 단원은 STL 사용하지 않고 배열로 생 구현 했다.
위 문제는 큐를 구현한 것.
본인은 배열에서 start와 last라는 두 변수를 사용하여 구현하였다.

Author

Yeonsang Shin

Posted on

2020-07-10

Updated on

2022-12-19

Licensed under

Comments