CodingSpace

[자료구조] 큐 (Queue) 본문

자료구조 및 알고리즘

[자료구조] 큐 (Queue)

개발자_조이킴 2023. 5. 23. 08:59


Queue

먼저 삽입된 데이터가 먼저 나오는 FIFO(First In First Out) 구조로

잘 알려진 자료구조이다.

 

너비 우선 탐색 (Breadth First Search, BFS)를 구현하는데 사용된다.

 

큐 (위키피디아)


코드 구현

자료구조 Queue를 javascript 코드로 구현하면 아래와 같다.

// javascript로 구현한 
// 자료구조 Queue

class Queue {
	constructor() {
    	this.queue = [];
        this.head = 0;
        this.rear = 0;
    }
    // 삽입
    enqueue(value) {
    	this.rear++;
        this.queue[this.rear] = value;
    }
    // 제거
    dequeue() {
    	const value = this.queue[this.front];
        delete this.queue[this.front];
        this.front = this.front + 1;
    }
    // 큐가 비어있는지
    isEmpty() {
    	return this.head === this.rear;
    }
}

References.

  • 코딩테스트 광탈 방지 A to Z: JavaScript (이선협)

For Developer. 

  • 잘못되거나 부족한 부분이 있다면 언제든지 댓글 부탁드립니다 :)
Comments