ํฌ์ŠคํŠธ

Queue

Queue

๐Ÿ’ซ @TODO


ํ•œ์ชฝ ๋์—์„œ ์›์†Œ๋ฅผ ๋„ฃ๊ณ , ๋ฐ˜๋Œ€์ชฝ ๋์—์„œ ์›์†Œ๋ฅผ ๋บ„ ์ˆ˜ ์žˆ๋Š” ๊ตฌ์กฐ
FIFO First in First out

Restricted Structure
ํŠน์ • ์œ„์น˜์—์„œ๋งŒ ์›์†Œ๋ฅผ ๋„ฃ๊ฑฐ๋‚˜ ๋บ„ ์ˆ˜ ์žˆ๋Š” ์ œํ•œ๋œ ์ž๋ฃŒ๊ตฌ์กฐ (์Šคํƒ, ํ, ๋ฑ)

๋Œ€๊ธฐ ํ–‰๋ ฌ

  • ์„ฑ์งˆ
    • ์›์†Œ ์ถ”๊ฐ€ O(1)
    • ์›์†Œ ์ œ๊ฑฐ O(1)
    • ์ œ์ผ ์•ž/๋’ค ์›์†Œ ํ™•์ธ O(1)
    • ์ œ์ผ ์•ž/๋’ค๊ฐ€ ์•„๋‹Œ ๋‚˜๋จธ์ง€ ์›์†Œ๋“ค์˜ ํ™•์ธ/๋ณ€๊ฒฝ์ด โ€˜์›์น™์ ์œผ๋กœโ€™ ๋ถˆ๊ฐ€๋Šฅ

์ถ”๊ฐ€๋˜๋Š” ์ชฝ์„ rear ๋’ค์ชฝ
์ œ๊ฑฐ๋˜๋Š” ์ชฝ์„ front ์•ž์ชฝ

๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๋ณด๋‹ค ๋ฐฐ์—ด ๊ตฌํ˜„์ด ๋” ์‰ฌ์›€

์›ํ˜• ํ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const int MAX = 1000005;
int dat[MAX];
int head = 0, tail = 0;

void push(int x)
{
	dat[tail++] = x;
}

void pop()
{
	head++;
}

int front()
{
	return dat[head];
}

int back()
{
	return dat[tail-1];
}

int size()
{
	return tail - head;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <bits/stdc++.h>
using namespace std;

int main(void)
{
	queue<int> Q;
	Q.push(10); // 10
	Q.push(20); // 10 20
	Q.push(30); // 10 20 30
	cout << Q.size() << '\n'; // 3
	if(Q.empty()) cout << "Q is empty\n";
	else cout << "Q is not empty\n"; // Q is not empty
	Q.pop(); // 20 30
	cout << Q.front() << '\n'; // 20
	cout << Q.back() << '\n'; // 30
	Q.push(40); // 20 30 40
	Q.pop(); // 30 40
	cout << Q.front() << '\n'; // 30
}

BFS
Flood Fill

๋‘˜ ๋‹ค ๋‹จ๊ณจ ๋ฌธ์ œ

๐Ÿซง _

์ด ๊ธฐ์‚ฌ๋Š” ์ €์ž‘๊ถŒ์ž์˜ CC BY 4.0 ๋ผ์ด์„ผ์Šค๋ฅผ ๋”ฐ๋ฆ…๋‹ˆ๋‹ค.