-
https://www.acmicpc.net/problem/10845
10845번: 큐
첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지
www.acmicpc.net
#include <iostream> using namespace std; const int MAX = 2000001; class bojQueue { private: int* queueMem; int front, tail; int max, size; public: bojQueue() : front(0), tail(0), max(MAX), size(0) { queueMem = new int[MAX]; } ~bojQueue() { delete[] queueMem; } void Push(const int data); int Pop(); int Size(); bool Empty(); int Front(); int Back(); bool Full(); }; void bojQueue::Push(const int data) { queueMem[tail++] = data; size++; } int bojQueue::Pop() { if (Empty()) return -1; int temp = queueMem[front++]; size--; return temp; } int bojQueue::Size() { return size; } bool bojQueue::Empty() { return size == 0; } int bojQueue::Front() { return Empty() ? -1 : queueMem[front]; } int bojQueue::Back() { return Empty() ? -1 : queueMem[tail - 1]; } bool bojQueue::Full() { return size == max; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int N; cin >> N; string order[6] = { "push", "pop", "size", "empty", "front", "back"}; bojQueue bjQue; for (int i = 0; i < N; i++) { string order; cin >> order; if (order == "push") { int data = 0; cin >> data; bjQue.Push(data); } else if (order == "pop") cout << bjQue.Pop() << "\n"; else if (order == "size") cout << bjQue.Size() << "\n"; else if (order == "empty") cout << bjQue.Empty() << "\n"; else if (order == "front") cout << bjQue.Front() << "\n"; else if (order == "back") cout << bjQue.Back() << "\n"; } return 0; }
'코딩 테스트 > 백준' 카테고리의 다른 글
10866 덱 (0) 2022.02.15 1158 요세푸스 문제 (0) 2022.02.14 1406 에디터 (0) 2022.02.11 1874 스택 수열 (0) 2022.02.10 9012 괄호 (0) 2022.02.09