IMMERSIVE

Queue

큐는 스택과 달리 첫 번째로 추가된 항목에 먼저 접근하는 자료구조이다(First In First Out). 연산이 상수 시간이라는 점이 큐의 장점이다. 큐는 스택과 비슷하게 한 번에 한개의 항목에 접근할 수 있다는 한계가 있다. 큐는 알고리즘이 첫 번째로 추가된 항목만을 접근해야 하는 선입선출 방식으로 자료를 처리햐야 하는 경우에 사용한다.

  • push();
  • shift();

큐에 어떠한 항목을 추가하는 것을 인큐(enqueueing), 큐에서 항목을 제거하는 것을 디큐(dequeueing)라고 한다.

function Queue(array) {
  this.array = [];
  if (array) {
    this.array = array;
  }
}

Queue.prototype.getBuffer = function() {
  // 배열복사
  return this.array.slice();
}

Queue.prototype.isEmpty = function() {
  // 안에 값이 있는지 없는지 확인
  return this.array.length === 0;
}

Queue.prototype.peek = function() {
  // 맨 앞에 값이 무엇인지 확인
  return this.array[0];
}

Queue.prototype.enqueue = function(value) {
  // 값 추가하기
  return this.array.push(value);
}

Queue.prototype.dequeue = function() {
  // 값 제거하기
  return this.array.shift();
}

const queue1 = new Queue();
console.log(queue1);