Published 2020. 12. 29. 05:34
 

10845번: 큐

첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지

www.acmicpc.net

#include <iostream>
#include <string>

template<typename T>
struct Queue {
	T data[10000];
	int begin, end;
	
	Queue() : begin(0), end(0) {}
	
	void push(int num) {
		if (empty()) {
			data[begin] = num;
			end++;
		}
		else {
			data[end++] = num;
		}
	}
	
	int pop() { 
		if (empty()) { return -1; }
		return data[begin++];
	}

	int size() { 
		return end - begin;
	}

	bool empty() { 
		if (begin == end) { return true; }
		return false;
	}

	int front() {
		if (empty()) { return -1; }
		return data[begin];
	}

	int back() {
		if (empty()) { return -1; }
		return data[end-1]; 
	}
};

int main() {
	using namespace std; ios_base::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);

	Queue<int> que;
	int t; cin >> t;
	while (t--) {
		string cmd; cin >> cmd;
		if (cmd == "push") {
			int num; cin >> num;
			que.push(num);
		}
		else if (cmd == "pop") {
			cout << que.pop() << "\n";
		}
		else if (cmd == "size") {
			cout << que.size() << "\n";
		}
		else if (cmd == "empty") {
			cout << que.empty() << "\n";
		}
		else if (cmd == "front") {
			cout << que.front() << "\n";
		}
		else if (cmd == "back") {
			cout << que.back() << "\n";
		}
		else if (cmd == "break") { break; }
	}
}

'Problem set' 카테고리의 다른 글

[백준] 10870: 피보나치 수 5  (0) 2020.12.29
[백준] 10866: 덱  (0) 2020.12.29
[백준] 10844: 쉬운 계단 수  (0) 2020.12.29
[백준] 10828: 스택  (0) 2020.12.29
[백준] 10818: 최소, 최대  (0) 2020.12.29
복사했습니다!