-
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๋ฐ์ํ๋๊ธ