정수를 저장하는 큐를 구현한 다음, 입력으로 주어지는 명령을 처리하는 프로그램을 작성하시오.
명령은 총 여섯 가지이다.
- push X: 정수 X를 큐에 넣는 연산이다.
- pop: 큐에서 가장 앞에 있는 정수를 빼고, 그 수를 출력한다. 만약 큐에 들어있는 정수가 없는 경우에는 -1을 출력한다.
- size: 큐에 들어있는 정수의 개수를 출력한다.
- empty: 큐가 비어있으면 1, 아니면 0을 출력한다.
- front: 큐의 가장 앞에 있는 정수를 출력한다. 만약 큐에 들어있는 정수가 없는 경우에는 -1을 출력한다.
- back: 큐의 가장 뒤에 있는 정수를 출력한다. 만약 큐에 들어있는 정수가 없는 경우에는 -1을 출력한다.
기본적인 큐는 뭐.. 기능만 구현해주면 된다.
명령어마다 함수로 나눠서 좀 길어졌긴 하지만 별 내용은 없다.
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 | // 10845 큐 #include<stdio.h> #include<string.h> void queue_push(int X); int queue_pop(); int queue_size(); int queue_empty(); int queue_front(); int queue_back(); int queue[10001]; int front, back; int main() { int N, num; char str[6]; scanf("%d", &N); while (N--) { scanf("%s", &str); if (!strcmp(str, "push")) { scanf("%d", &num); queue_push(num); } else if (!strcmp(str, "pop")) printf("%d\n",queue_pop()); else if (!strcmp(str, "size")) printf("%d\n", queue_size()); else if (!strcmp(str, "empty")) printf("%d\n", queue_empty()); else if (!strcmp(str, "front")) printf("%d\n", queue_front()); else if (!strcmp(str, "back")) printf("%d\n", queue_back()); } return 0; } void queue_push(int X) { queue[back] = X; back++; } int queue_pop() { if (front == back) return -1; return queue[front++]; } int queue_size() { return back - front; } int queue_empty() { if (front == back) return 1; return 0; } int queue_front() { if (front == back) return -1; return queue[front]; } int queue_back() { if (front == back) return -1; return queue[back-1]; } |
'알고리즘' 카테고리의 다른 글
BOJ 1966 프린터 큐 (0) | 2018.10.04 |
---|---|
BOJ 1260 DFS와 BFS (0) | 2018.10.04 |
BOJ 2504 괄호의 값 (0) | 2018.10.04 |
BOJ 9012 괄호 (0) | 2018.09.01 |
BOJ 1874 스택 수열 (0) | 2018.09.01 |