π STUDY/JAVA
QUEUE
JuneBee
2022. 5. 18. 10:42
728x90
λ°μν
νΉμ±
- μ€νκ³Ό λ§μ°¬κ°μ§λ‘ μ½μ κ³Ό μμ μ μμΉκ° μ νμ μΈ μλ£κ΅¬μ‘°
- μ μ
μ μΆ κ΅¬μ‘° (First In First Out)
νμ μ½μ ν μμλλ‘ μμκ° μ μ₯λλ©°, κ°μ₯ λ¨Όμ μ½μ λ μμκ° κ°μ₯ λ¨Όμ μμ λ¨ - κΈ°λ³Έ μ°μ° : enQueue, deQueue μ£Όμ λ©μλ: offer(), poll(), isEmpty(), size(), peek()
- μ£Όλ‘ LinkedList ν΄λμ€λ₯Ό Queue μΈν°νμ΄μ€μ ꡬνμ²΄λ‘ λ§μ΄ μ¬μ©νλ€
π€¦π»βοΈ ν μμ μ무κ²λ μμ λ, μ€νκ³Ό λ§μ°¬κ°μ§λ‘
poll() : μμ - λ₯Ό λΆλ μ λ, μμΈ λ°μ μ²λ¦¬κ° μλ€
remove() : μμ - λ₯Ό λΆλ μ λ, μμΈκ° λ°μνλ€
μμ
import jvava.util.Queue;
import java.util.LinkedList;
public class QueueTest{
public static void main(String [] args){
int N = 10;
Queue <int[]> queue = new LinkedList<>(); //μλ λ²νΈ, λΆμ΄λΉ΅ κ°―μ
int customer = 1;
queue.offer(new int[] {customer, 1});
while (N >0){
if(!queue.isEmpty()){
int[] p = queue.poll();
int buyable = (N>p[1])? p[1]: N; //μ΄μμλ λΆμ΄λΉ΅ μ
N -= buyable ; //νλ¦° κ°―μ
if(N==0){//μ λΆ νλ¦Ό
System.out.println("λ§μ§λ§ μλ: "+ p[0] +"μ¬κ° κ°―μ: "+buyable); }
else{
System.out.println(p[0]+"λ² μλμ΄ " + buyable+ "λ§νΌ λΆμ΄λΉ΅μ μ¬κ°μ΅λλ€\\nλ¨μκ°μ: "+N);
p[1] ++; //μ¬λ €λ λΆμ΄λΉ΅ μ¦κ°
queue.offer(p);
queue.offer(new int[] {++customer,1}); }
}//end of isEmpty
} //end of while
} //end of main
} //end of class
728x90
λ°μν