10845_큐(C++)

1487 단어 큐(Queue)큐(Queue)
#include <iostream>
using namespace std;

struct Queue
{
	int queue[10000];
	int begin = 0;
	int end = 0;

	void push(int data) {
		queue[end] = data;
		end++;
	}
	void pop() {
		if (!empty()) {
			cout << queue[begin] << '\n';
			begin++;
		}
		else cout << -1 << '\n';
	}
	void size() {
		cout << end - begin << '\n';
	}
	bool empty() {
		if (begin == end) return 1;
		else return 0;
	}
	void front() {
		if (!empty()) cout << queue[begin] << '\n';
		else cout << -1 << '\n';
	}
	void back() {
		if (!empty()) cout << queue[end - 1] << '\n';
		else cout << -1 << '\n';
	}

};
int main() {
	int N;
	string command;
	Queue mine;
	cin >> N;
	for (int i = 0; i < N; i++) {
		cin >> command;
		if (command == "push") {
			int k = 0;
			cin >> k;
			mine.push(k);
		}
		else if (command == "pop") {
			mine.pop();
		}
		else if (command == "size") {
			mine.size();
		}
		else if (command == "empty") {
			cout << mine.empty() << '\n';
		}
		else if (command == "front") {
			mine.front();
		}
		else if (command == "back") {
			mine.back();
		}
	}
}

큐 설명 참고:
https://velog.io/@kimeunseo58/%ED%81%90

좋은 웹페이지 즐겨찾기