#include <iostream>

using namespace std;

class Queue {
private:
	int arr[10000];
	int head;
	int tail;

public:

	void init() {
		head = -1;
		tail = -1;
	}


	void push(int data) {

		if (head == -1)
			head++;

		arr[++tail] = data;

	}

	int size() {
		if (tail < 0)
			return 0;

		return tail - head + 1;

	}


	bool empty() {
		return  size() <= 0;
	}

	int pop() {
		if (empty())
			return -1;

		return arr[head++];
	}

	int front()
	{
		if (empty())
			return -1;

		return arr[head];
	}


	int back()
	{
		if (empty())
			return -1;


		return arr[tail];
	}



};

int main()
{

	int N;
	cin >> N;

	Queue q;
	q.init();

	string command;

	for (int i = 0; i < N; i++)
	{
		cin >> command;

		if (command == "push")
		{
			int data;
			cin >> data;
			q.push(data);

		}
		else if (command == "pop")
		{
			cout << q.pop() << endl;;
		}
		else if (command == "size")
		{
			cout << q.size() << endl;
		}
		else if (command == "empty")
		{
			cout << q.empty() << endl;
		}
		else if (command == "front")
		{
			cout << q.front() << endl;
		}
		else if (command == "back")
		{
			cout << q.back() << endl;
		}
		

	}




}

 

여러 가지 구현 방법이 있지만 스택 개념에서 확장하여 입구가 2개(앞,뒤)로 생각하여 구현 해봤다.

 

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

+ Recent posts