❶ 데크 (Deque)

❓ Double-Ended Queue의 약자로, 삽입과 삭제가 양쪽 끝에서 모두 발생할 수 있는 선형 자료 구조

❷ 데크 구현하기

양쪽에서 삽입과 삭제를 할 수 있기 때문에, 다른 자료구조와 달리 데이터 추가 및 삭제가 총 4개 이다.

(1) Deque() : 초기 속성값 설정을 위한 생성자 함수

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

(2) getBuffer() : 객체 내 데이터 셋 반환

Deque.prototype.getBuffer = function () {
  return this.array.slice();
}

(3) isEmpty() : 데이터가 비어 있는지 확인

Deque.prototype.isEmpty = function () {
  return this.array.length === 0;
}

(4) pushFront() : 앞 쪽 데이터 추가

Deque.prototype.pushFront = function (element) {
  return this.array.unshift(element);
}

(5) popFront() : 앞 쪽 데이터 삭제

Deque.prototype.popFront = function () {
  return this.array.shift();
}

(6) pushBack() : 뒤 쪽 데이터 추가

Deque.prototype.pushBack = function (element) {
  return this.array.push(element);
}

(7) popBack() : 뒤 쪽 데이터 삭제

Deque.prototype.popBack = function () {
  return this.array.pop();
}

(8) front() : 가장 첫 데이터 반환

Deque.prototype.front = function () {
  return this.array.length === 0 ? undefined : this.array[0];
}

(9) back() : 가장 끝 데이터 반환

Deque.prototype.back = function () {
  return this.array.length === 0
    ? undefined
    : this.array[this.array.length - 1];
}

(10) size() : 큐 내 데이터 개수 확인

Deque.prototype.size = function () {
  return this.array.length;
}

(11) clear() : 큐 초기화

Deque.prototype.clear = function () {
  this.array = [];
}
profile
#UXUI #코린이

0개의 댓글