![[C++] 백준 - 10845번 문제 (큐)](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2Fz2KLK%2FbtsluTzf4Iy%2Fiwy6MK7CNICUa7tesyUmw0%2Fimg.png)
[C++] 백준 - 10845번 문제 (큐)자료구조 & 알고리즘/BOJ2023. 6. 26. 20:42
Table of Contents
문제설명
소스코드
#include<iostream>
using namespace std;
int max = 10000; //큐 용량
int num = 0; //현재 데이터 수
int que[10000];
bool isFull() { return ::num >= ::max; } //큐의 공간이 모두 사용중인지 리턴
bool isEmpty() { return ::num <= 0; } //큐의 공간이 비어있는지 리턴
void enque(int value)
{
if (!isFull()) ::que[num++] = value;
}
int size() { return ::num; } //큐의 현재 데이터 수를 리턴
void deque()
{
if (!isEmpty())
{
for (int i = 1; i < num; ++i)
{
que[i - 1] = que[i];
}
que[--num] = 0;
}
}
int front() { if (num > 0) return que[0]; }
int main()
{
int N;
cin >> N;
for (int i = 0; i < N; ++i)
{
string command = "";
int value;
cin >> command;
if (command == "push") { cin >> value; enque(value); }
else if (command == "pop")
{
if (isEmpty()) cout << "-1" << endl;
else
{
cout << front() << endl;
deque();
}
}
else if (command == "size") cout << size() << endl;
else if (command == "empty") (isEmpty() == true) ? cout << 1 << endl : cout << 0 << endl;
else if (command == "front") (isEmpty() == true) ? cout << -1 << endl : cout << front() << endl;
else if (command == "back") (isEmpty() == true) ? cout << -1 << endl : cout << que[num - 1] << endl;
}
}
설명
2023.01.29 - [자료구조 & 알고리즘] - [JAVA] 큐(Queue) / 링 버퍼(Ring Buffer) / 데크(Deque)
[JAVA] 큐(Queue) / 링 버퍼(Ring Buffer) / 데크(Deque)
Do it! 자료구조와 함께 배우는 알고리즘 입문[자바편] 연습문제와 실습문제입니다. 큐 큐는 스택과 마찬가지로 데이터를 일시적으로 쌓아 놓은 자료구조이다. 하지만 큐는 스택과 다르게 선입선
rebugs.tistory.com
'자료구조 & 알고리즘 > BOJ' 카테고리의 다른 글
[C++] 백준 12단계 - 19532번 문제 (수학은 비대면강의입니다) (0) | 2023.07.10 |
---|---|
[C++] 백준 20단계 - 18258번 문제 (큐 2) (0) | 2023.07.10 |
[C++] 백준 6단계 - 4344번 문제 (평균은 넘겠지) (0) | 2023.06.26 |
[C++] 백준 12단계 - 2231번 문제 (분해합) (0) | 2023.06.25 |
[C++] 백준 12단계 - 2798번 문제 (블랙잭) (0) | 2023.06.25 |