자료구조 Queue

양희준·2022년 4월 30일
0

백준 문제 큐를 참고해서 자바스크립트로 만든 자료구조 Queue 입니다.
백준 10845: 큐

// 자료 구조 Queue 생성 (Class 이용)

class Queue {
    constructor() {
        // 데이터를 저장할 저장소 초기화
        this.storage = {};
        // 맨 앞의 포인터 초기화
        this.head = 0;
        // 맨 뒤의 포인터 초기화
        this.tail = 0;
    }
    // Queue에 담겨있는 요소들의 개수를 출력하는 함수
    size() {
        // Queue에 담겨있는 요소가 없다면 0을 반환
        if(this.storage[this.tail] === undefined) return 0;
        // Queue에 담겨있는 요소가 있다면 요소의 개수를 반환
        else return this.tail - this.head + 1;
    }
    // Queue의 맨 뒤에 value를 넣는 함수
    push(value) {
        // 예외처리 처음 비어져있는 Queue일 경우
        if(this.size() === 0) {
            this.storage[this.head] = value;
        } else {
            // tail를 1 더하고 tail를 인덱스 번호로 하는 스토리지에 해당 값을 저장
            this.tail += 1;
            this.storage[this.tail] = value;
        }
    }
    // Queue의 맨 앞에 value를 빼주는 함수
    pop() {
        // 예외처리 Queue가 비어져 있는 경우
        if(this.size() === 0) {
            return undefined;
            // head와 tail이 같은 경우
        } else if(this.head === this.tail) {
            // delete 연산자로 해당 값 삭제
            delete this.storage[this.head];
            // 변수 초기화
            this.head = 0;
            this.tail = 0;
        } else {
            delete this.storage[this.head];
            this.head += 1;
        }
    }
    // Queue에 요소가 있는지 없는지 확인하는 함수
    empty() {
        // 요소가 있을 경우 ture를 출력
        if(this.size() !== 0) return true;
        // 요소가 없을 경우 false를 출력
        else return false;
    }
    // Queue의 맨 앞의 요소를 출력하는 함수
    front() {
        // Queue에 요소가 없다면 undefined 반환
        if(this.size() === 0) return undefined;
        // 요소가 있다면 제일 앞의 요소를 반환
        else return this.storage[this.head];
    }
    // Queue의 맨 뒤의 요소를 출력하는 함수
    back() {
        // Queue에 요소가 없다면 undefined 반환
        if(this.size() === 0) return undefined;
        // 요소가 있다면 제일 뒤의 요소를 반환
        else return this.storage[this.tail];
    }
}
// 자료 구조 Queue 생성 (생성자 함수 이용)

function Queue() {
    this.storage = {};
    this.head = 0;
    this.tail = 0;
}

Queue.prototype.size = function() {
    if(this.storage[this.tail] === undefined) return 0;
    else return this.tail - this.head + 1;
}

Queue.prototype.push = function(value) {
    if(this.size() === 0) {
        this.storage[this.head] = value;
    } else {
        this.tail += 1;
        this.storage[this.tail] = value;
    }
}

Queue.prototype.pop = function() {
    if(this.size() === 0) {
        return undefined;
    } else if(this.head === this.tail) {
        delete this.storage[this.head];
        this.head = 0;
        this.tail = 0;
    } else {
        delete this.storage[this.head];
        this.head += 1;
    }
}

Queue.prototype.empty = function() {
    if(this.size() !== 0) return true;
    else return false;
}

Queue.prototype.front = function() {
    if(this.size() === 0) return undefined;
    else return this.storage[this.head];
}

Queue.prototype.back = function() {
    if(this.size() === 0) return undefined;
    else return this.storage[this.tail];
}
profile
JS 코린이

0개의 댓글