😎 κ³΅λΆ€ν•˜λŠ” μ§•μ§•μ•ŒνŒŒμΉ΄λŠ” μ²˜μŒμ΄μ§€?

[이것이 μ½”λ”© ν…ŒμŠ€νŠΈλ‹€ with Python]_16_μŠ€νƒκ³Ό 큐 λ³Έλ¬Έ

πŸ¦₯ μ½”ν…Œ/이것이 μ½”λ”© ν…ŒμŠ€νŠΈλ‹€ with python

[이것이 μ½”λ”© ν…ŒμŠ€νŠΈλ‹€ with Python]_16_μŠ€νƒκ³Ό 큐

μ§•μ§•μ•ŒνŒŒμΉ΄ 2022. 2. 2. 00:24
728x90
λ°˜μ‘ν˜•

220202 μž‘μ„±

<λ³Έ λΈ”λ‘œκ·ΈλŠ” γ€Žμ΄κ²ƒμ΄ 취업을 μœ„ν•œ μ½”λ”© ν…ŒμŠ€νŠΈλ‹€γ€ μ˜ youtubeλ₯Ό μ°Έκ³ ν•΄μ„œ κ³΅λΆ€ν•˜λ©° μž‘μ„±ν•˜μ˜€μŠ΅λ‹ˆλ‹€>

https://www.youtube.com/watch?v=7iLoLcna7Hw&list=PLVsNizTWUw7H9_of5YCB0FmsSc-K44y81&index=16 

 

 

 

 

 

 

1. 탐색

: λ§Žμ€ μ–‘μ˜ 데이터 μ€‘μ—μ„œ μ›ν•˜λŠ” 데이터λ₯Ό μ°ΎλŠ” κ³Όμ •

ex) DFS, BFS

 

 

 

 

2. μŠ€νƒ

: λ¨Όμ € λ“€μ–΄ 온 데이터가 λ‚˜μ€‘μ— λ‚˜κ°€λŠ” ν˜•μ‹ (μ„ μž…ν›„μΆœ)

: μž…κ΅¬μ™€ μΆœκ΅¬κ°€ λ™μΌν•œ ν˜•νƒœ

  • python
stack = []

stack.append(5)
stack.append(3)
stack.append(2)
stack.append(1)
stack.pop()
stack.append(4)
stack.append(7)
stack.pop()

print(stack[::-1])    # μ΅œμƒλ‹¨ λΆ€ν„°
print(stack)          # μ΅œν•˜λ‹¨ λΆ€ν„°



# result
# [4, 2, 3, 5]
# [5, 3, 2, 4]

=> <μ΅œν•˜, 처음> 5, 3, 2, 1(μ‚­μ œ), 4, 7(μ‚­μ œ) <μ΅œμƒ, λ‚˜μ€‘>

 

  • c++
#include <bits/stdc++.h>
using namespace std;

stack<int> s;

int main(void) {
	s.push(3);
    s.push(2);
    s.push(1);
    s.push(5);
    s.pop();
    s.push(3);
    s.push(1);
    s.pop();
    
    while (!s.empty()) {
    	cout << s.top() << ' ';		# top : μ΅œμƒλ‹¨λΆ€ν„°
        s.pop();
    }
}
  • java
import java.util.*;

public class Main {
	
    public static void main(String[] args) {
    	Stack<Integer> s = new Stack<>();
        
		s.push(4);
    	s.push(5);
    	s.push(2);
        s.push(1);
        s.pop();
    	s.push(7);
    	s.push(8);
	    s.pop();
    
    	while (!s.empty()) {
    		System.out.print(s.peek() + " ");	# peek : μ΅œμƒλ‹¨λΆ€ν„°
        	s.pop();
    	}
    }
}

 

 

 

 

 

3. 큐

: λ¨Όμ € λ“€μ–΄ 온 데이터가 λ¨Όμ € λ‚˜κ°€λŠ” ν˜•μ‹ (μ„ μž…μ„ μΆœ)

: μž…κ΅¬μ™€ μΆœκ΅¬κ°€ λͺ¨λ‘ 뚫렀 μžˆλŠ” 터널

 

 

  • python
from collections import deque

queue = deque()

queue.append(5)
queue.append(4)
queue.append(1)
queue.append(2)
queue.popleft()
queue.append(2)
queue.append(3)
queue.popleft()

print(queue)    # λ¨Όμ € λ“€μ–΄μ˜¨ μ• λΆ€ν„°
queue.reverse()
print(queue)    # λ‚˜μ€‘μ— λ“€μ–΄μ˜¨ μ• λΆ€ν„°


# result
# deque([1, 2, 2, 3])
# deque([3, 2, 2, 1])

=> <μ΅œν•˜, 처음> 5(μ‚­μ œ1), 4(μ‚­μ œ2), 1, 2, 2, 3 <μ΅œμƒ, λ‚˜μ€‘>

 

  • c++
#include <bits/stdc++.h>
using namespace std;

queue<int> q;

int main(void) {
	q.push(3);
    q.push(2);
    q.push(3);
    q.push(4);
    q.pop();
    q.push(1);
    q.push(2);
    q.pop();
    
    while (!q.empty())	{
    	cout << q.front() << ' ';
        q.pop()
    }
}
  • java
import java.util.*;

public class Main {

	public static void main(String[] args) {
    	Queue<Integer> q = new LinkedList<>();
        
        q.offer(2);
        q.offer(5);
        q.offer(6);
        q.offer(3);
        q.poll();
        q.offer(8);
        q.offer(3);
        q.poll();

		while (!q.empty())	{
    		System.out.print(q.poll() + " ");
    	}
    }
}

 

 

 

 

 

 

 

 

 

 

 

728x90
λ°˜μ‘ν˜•
Comments