[백준18258_자바스크립트(javascript)] - 큐 2

경이·2024년 6월 15일

𝑩𝑶𝑱 (𝒋𝒔)

목록 보기
68/325

🔴 문제

큐 2


🟡 Sol

const fs = require('fs');
const path = process.platform === 'linux' ? '/dev/stdin' : 'Wiki\\input.txt';
const [n, ...input] = fs.readFileSync(path).toString().trim().split('\r\n');

const command = input.map((it) => it.split(' '));
const que = [];
let popped = 0;
let result = '';

for (cmd of command) {
  let queLen = (que.length - popped).toString();
  switch (cmd[0]) {
    case 'size':
      result += queLen + '\n';
      break;
    case 'empty':
      result += (queLen == '0' ? '1' : '0') + '\n';
      break;
    case 'front':
      result += (queLen == '0' ? '-1' : que[popped]) + '\n';
      break;
    case 'back':
      result += (queLen == '0' ? '-1' : que[que.length - 1]) + '\n';
      break;
    case 'pop':
      if (queLen === '0') result += '-1\n';
      else {
        result += que[popped] + '\n';
        popped++;
      }
      break;
    case 'push':
      que.push(cmd[1]);
  }
}

console.log(result);

🟢 풀이

문제에서 요구하는대로 풀이하면 된다.
단 push나 pop 같은 경우 연산자와 숫자를 같이 주어주기 때문에 연산자만 체크하는 과정이 필요하다. 결과값은 result라는 문자열 변수에 차곡차곡 더해주면 된다.


🔵 Ref

profile
록타르오가르

0개의 댓글