JS로 Function queue 구현하기

ES5 프로토타입 상속 문법

export function FQueue() {
    this.inputSt = [];
    this.outputSt = [];
    this.size = 0;
    
    this.enque = (value) => {
        this.inputSt.push(value);
        this.size++;
    }
    

    this.deque = () => {
      
        if(this.outputSt.length === 0) {
            this.outputSt = this.inputSt.reverse();
            this.inputSt = [];
        }
        
        const top = this.outputSt[this.outputSt.length-1];
        this.outputSt.pop();
        this.size--;
        return top;
    }
}

JS로 Class queue 구현하기

export class CQueue {   
    constructor() {
        this.inputSt = [];
        this.outputSt = [];
        this.size = 0;
    }

    enque(value) {
        this.inputSt.push(value);
        this.size++;
    }
    
    deque() {
        if(this.outputSt.length === 0) {
            this.outputSt = this.inputSt.reverse();
            this.inputSt = [];
        }    
        const top = this.outputSt[this.outputSt.length-1];
        this.outputSt.pop();
        this.size--;
        return top;
    }
}

결과 비교

import {FQueue} from "./FQueue.js"
import {CQueue} from "./CQueue.js";

const fQue = new FQueue();
const cQue = new CQueue();

const input = [1,2,3,4,5];

console.log('-------------FQueue-------------------');
input.forEach(val => fQue.enque(val));

while(fQue.size > 0) {
    fQue.deque();
}
console.log('--------------------------------------');

console.log('-------------CQueue-------------------');

input.forEach(val => cQue.enque(val));
while(cQue.size > 0) {
    cQue.deque();
}

console.log('--------------------------------------');

활용하기

현재 대기목록에 있는 문서의 중요도가 순서대로 담긴 배열 priorities와 내가 인쇄를 요청한 문서가 현재 대기목록의 어떤 위치에 있는지를 알려주는 location이 매개변수로 주어질 때, 내가 인쇄를 요청한 문서가 몇 번째로 인쇄되는지 return 하도록 solution 함수를 작성해주세요.

class MyQueue {
    constructor() {
        this.inputSt = [];
        this.outputSt = [];
        this.size = 0;
    }
    
    enque(value) {
        this.inputSt.push(value);
        this.size++;
    }
    
    deque() {
        if(this.outputSt.length === 0) {
            this.outputSt = this.inputSt.reverse();
            this.inputSt = [];
        }
        const top = this.outputSt[this.outputSt.length-1];
        this.outputSt.pop();
        this.size--;
        return top;
    }
}

function solution(priorities, location) {
    
    const que = new MyQueue();
    
    priorities.forEach((p, loc) => que.enque([p, loc]));
    priorities.sort((a,b) => b-a);
    
    const printLoc = [];
    let printedCnt = 0;
    
    while(que.size > 1) {
        const [peek, loc] = que.deque();
        
        if(peek < priorities[printedCnt]) {
           que.enque([peek, loc]); 
           continue;
        }
        
        printedCnt++;
        printLoc.push(loc);
    }
    
    return printLoc.indexOf(location)+1;
}

풀이

  1. Queue를 사용하기 위해 que 인스턴스를 생성한다.
  2. peek와 location의 정보를 함께 que에 enque한다.
  3. 우선순위를 내림차순으로 정렬한다.
  4. que에 크기가 있다면 enque한 peek와 loc을 deque 한다.
  5. peek가 프린트할 순서와 비교해 작을 경우 peek를 다시 enque한다.
  6. 그런 경우가 아니라면 printCnt에 1을 더하고 프린트 배열에 위치값을 넣는다.