
#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개(앞,뒤)로 생각하여 구현 해봤다.
'Algorithm' 카테고리의 다른 글
| [프로그래머스] 추억 점수 (0) | 2025.04.08 |
|---|---|
| [프로그래머스] 달리기 경주 (0) | 2025.04.08 |
| [자료구조] 스택 (0) | 2025.04.02 |
| [프로그래머스] [PCCP 기출문제] 1번 / 동영상 재생기 (0) | 2025.04.01 |
| [프로그래머스] 올바른 괄호 (1) | 2025.03.31 |