데크 (Deque) 구현해보기

Lainlnya·2022년 9월 27일
0

알고리즘

목록 보기
10/25

데크란?

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

데크

구현 메서드

  • 데이터 전체 획득 / 비어 있는지 확인: Deque.getBuffer(), Deque.isEmpty()
  • 데이터 추가 / 삭제: Deque.pushFront(), Deque.popFront(), Deque.pushBack(), Deque.popBack()
  • 첫번째 & 끝 데이터 반환 / 사이즈 / 전체 삭제: Deque.front(), Deque.back(), Deque.size(), Deque.clear()

대부분의 메서드는 큐와 동일하나, 앞에서 추가 삭제, 뒤에서 추가 삭제, 첫번째와 끝의 데이터를 반환하는 메서드가 추가됨

1. 초기 속성값 설정을 위한 생성자 함수

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

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

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

3. 객체 내 데이터의 존재 여부 확인 (Deque.isEmpty())

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

4. 앞 쪽 데이터 추가 (Deque.pushFront(element))

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

5. 앞 쪽 데이터 삭제 (Deque.popFront())

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

6. 뒤 쪽 데이터 추가 (Deque.pushBack(element))

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

7. 뒤 쪽 데이터 삭제 (Deque.popBack())

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

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

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

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

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

10. 크기 반환 (Deque.size())

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

11. 데크 초기화 (Deque.clear())

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

관련 전체 코드는 Git에 업로드 해두었습니다.
Github_Deque

profile
Growing up

0개의 댓글