백준 c++/(1-1)백준 c++ 알고리즘 기초

백준 10845 c++ 큐

현구구 2022. 7. 4. 16:32

https://www.acmicpc.net/problem/10845

 

10845번: 큐

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

www.acmicpc.net

#include <iostream>
#include <string>
#include <queue>
using namespace std;

int main()
{
	queue<int> queue1;
	int count;
	cin >> count;
	string s;
	while (count--)//count 횟수 반복
	{
		cin >> s;//string 입력받음
		if (s == "push")//push일경우
		{
			int pushX;//int형 입력받고 push
			cin >> pushX;
			queue1.push(pushX);
		}
		if (s == "front")//front의 경우 push가 가장 오래된 값
		{
			if (queue1.empty() == 1)
				cout << -1 << '\n';
			else
				cout << queue1.front() << '\n';
		}
		if (s == "back")//back의 경우 가장 최근에 push된 값
		{
			if (queue1.empty() == 1)
				cout << -1 << '\n';
			else
				cout << queue1.back() << '\n';
		}
		if (s == "size")
		{
			cout << queue1.size() << '\n';
		}
		if (s == "empty")
		{
			if (queue1.empty() == 1)
				cout << 1 << '\n';
			else if (queue1.empty() == 0)
				cout << 0 << '\n';
		}
		if (s == "pop")//pop의 경우 push가 가장 오래된 값(front의 값) push
		{
			if (queue1.empty() == 1)
				cout << -1 << '\n';
			else
			{
				cout << queue1.front() << '\n';
				queue1.pop();
			}
		}
	}
}

큐의 경우 스택과 달리 push는 back으로, pop은 front로 된다.