front() : 큐 첫번째의 원소를 반환하고 해당 원소 제거를 제거 하지 않음 ( 스택 변경 X)
isEmpty() : 큐 원소가 한개도 없으면 True / 스택 크기가 0보다 크면 False 반환
size() : 큐의 원소 개수 반환
4) Queue 클래스
functionQueue(){letitems=[];this.enqueue=function(element){items.push(element);}this.dequeue=function(){// shift()는 배열의 첫번째 요소를 제거하고 반환함 / pop()은 배열의 마지막 요소를 제거하고 반환함returnitems.shift();}this.front=function(){returnitems[0];}this.isEmpty=function(){returnitems.length===0;}this.size=function(){returnitems.length;}this.clear=function(){items=[];}this.print=function(){console.log(items.toString());}}letqueue=newQueue();console.log(queue.isEmpty());// outputs truequeue.enqueue("DongNyeong");queue.enqueue("DongNyeong2");queue.enqueue("DongNyeong3");queue.print();// outputs [DongNyeong, DongNyeong2, DongNyeong3]console.log(queue.size());// outputs 3console.log(queue.isEmpty());// outputs falsequeue.dequeue();//output DongNyeong removed DongNyeong from the queuequeue.dequeue();//output DongNyeong2 removed DongNyeong2 from the queuequeue.print();// outputs DongNyeong3
2) 우선순위 큐 활용
비행기 퍼스트 클래스 / 비즈니스 클래스 / 이코노미 클래스 순으로 우선순위를 매기는 경우
뜨거운 감자를 옆 사람에게 최대한 빠르게 전달하다 , 갑자기 동작을 멈추고 뜨거운 감자를 손에 들고 있는 아이를 벌칙으로 퇴장 시키는 게임
functionhotPotato(nameList,num){letqueue=newQueue();for(leti=0;i<nameList.length;i++){queue.enqueue(nameList[i]);}leteliminated='';while(queue.size()>1){for(leti=0;i<num;i++){queue.enqueue(queue.dequeue());}eliminated=queue.dequeue();console.log(eliminated+'님이 Hot Potato 게임에서 제외되었습니다.');}returnqueue.dequeue();}letnames=['dongnyeong1','dongnyeong2','dongnyeong3','dongnyeong4','dongnyeong5'];letwinner=hotPotato(names,7);console.log('승자는: '+winner+'입니다.');//승자는: dongnyeong1입니다.//dongnyeong3님이 Hot Potato 게임에서 제외되었습니다.//dongnyeong2님이 Hot Potato 게임에서 제외되었습니다.//dongnyeong5님이 Hot Potato 게임에서 제외되었습니다.//dongnyeong4님이 Hot Potato 게임에서 제외되었습니다.//승자는: dongnyeong1입니다.