mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-02 03:05:24 +08:00
feat(ko): add the Korean online edition (#2006)
* feat(ko): add the complete Korean online edition * Use UTF-8 for Python CI output on Windows * Remove print-book promotion from the Korean site * fix(ko): preserve emphasis before Korean particles
This commit is contained in:
@@ -0,0 +1,156 @@
|
||||
/**
|
||||
* File: array_deque.js
|
||||
* Created Time: 2023-02-28
|
||||
* Author: Zhuo Qinyue ([email protected])
|
||||
*/
|
||||
|
||||
/* 원형 배열로 구현한 덱 */
|
||||
class ArrayDeque {
|
||||
#nums; // 덱의 원소를 저장하는 배열
|
||||
#front; // 맨 앞 원소를 가리키는 맨 앞 포인터
|
||||
#queSize; // 덱 길이
|
||||
|
||||
/* 생성자 */
|
||||
constructor(capacity) {
|
||||
this.#nums = new Array(capacity);
|
||||
this.#front = 0;
|
||||
this.#queSize = 0;
|
||||
}
|
||||
|
||||
/* 덱의 용량 반환 */
|
||||
capacity() {
|
||||
return this.#nums.length;
|
||||
}
|
||||
|
||||
/* 덱의 길이 반환 */
|
||||
size() {
|
||||
return this.#queSize;
|
||||
}
|
||||
|
||||
/* 덱이 비어 있는지 판단 */
|
||||
isEmpty() {
|
||||
return this.#queSize === 0;
|
||||
}
|
||||
|
||||
/* 원형 배열 인덱스 계산 */
|
||||
index(i) {
|
||||
// 나머지 연산을 통해 배열의 처음과 끝을 연결한다
|
||||
// i가 배열의 끝을 넘어가면 처음으로 돌아온다
|
||||
// i가 배열의 처음을 넘어가면 끝으로 돌아온다
|
||||
return (i + this.capacity()) % this.capacity();
|
||||
}
|
||||
|
||||
/* 맨 앞에 삽입 */
|
||||
pushFirst(num) {
|
||||
if (this.#queSize === this.capacity()) {
|
||||
console.log('덱이 가득 찼다');
|
||||
return;
|
||||
}
|
||||
// 맨 앞 포인터를 왼쪽으로 한 칸 이동한다
|
||||
// 나머지 연산을 통해 front가 배열의 처음을 넘어가면 끝으로 돌아오게 한다
|
||||
this.#front = this.index(this.#front - 1);
|
||||
// num을 맨 앞에 추가한다
|
||||
this.#nums[this.#front] = num;
|
||||
this.#queSize++;
|
||||
}
|
||||
|
||||
/* 맨 뒤에 삽입 */
|
||||
pushLast(num) {
|
||||
if (this.#queSize === this.capacity()) {
|
||||
console.log('덱이 가득 찼다');
|
||||
return;
|
||||
}
|
||||
// 맨 뒤 인덱스 + 1을 가리키는 맨 뒤 포인터를 계산한다
|
||||
const rear = this.index(this.#front + this.#queSize);
|
||||
// num을 맨 뒤에 추가한다
|
||||
this.#nums[rear] = num;
|
||||
this.#queSize++;
|
||||
}
|
||||
|
||||
/* 맨 앞에서 꺼내기 */
|
||||
popFirst() {
|
||||
const num = this.peekFirst();
|
||||
// 맨 앞 포인터를 뒤로 한 칸 이동한다
|
||||
this.#front = this.index(this.#front + 1);
|
||||
this.#queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* 맨 뒤에서 꺼내기 */
|
||||
popLast() {
|
||||
const num = this.peekLast();
|
||||
this.#queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* 맨 앞 원소 접근 */
|
||||
peekFirst() {
|
||||
if (this.isEmpty()) throw new Error('덱이 비어 있다.');
|
||||
return this.#nums[this.#front];
|
||||
}
|
||||
|
||||
/* 맨 뒤 원소 접근 */
|
||||
peekLast() {
|
||||
if (this.isEmpty()) throw new Error('덱이 비어 있다.');
|
||||
// 마지막 원소의 인덱스를 계산한다
|
||||
const last = this.index(this.#front + this.#queSize - 1);
|
||||
return this.#nums[last];
|
||||
}
|
||||
|
||||
/* 출력에 사용할 배열 반환 */
|
||||
toArray() {
|
||||
// 유효한 길이 범위 안의 리스트 원소만 변환한다
|
||||
const res = [];
|
||||
for (let i = 0, j = this.#front; i < this.#queSize; i++, j++) {
|
||||
res[i] = this.#nums[this.index(j)];
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
/* 실행 코드 */
|
||||
/* 덱 초기화 */
|
||||
const capacity = 5;
|
||||
const deque = new ArrayDeque(capacity);
|
||||
deque.pushLast(3);
|
||||
deque.pushLast(2);
|
||||
deque.pushLast(5);
|
||||
console.log('덱 deque = [' + deque.toArray() + ']');
|
||||
|
||||
/* 원소 접근 */
|
||||
const peekFirst = deque.peekFirst();
|
||||
console.log('맨 앞 원소 peekFirst = ' + peekFirst);
|
||||
const peekLast = deque.peekLast();
|
||||
console.log('맨 뒤 원소 peekLast = ' + peekLast);
|
||||
|
||||
/* 큐에 원소 삽입 */
|
||||
deque.pushLast(4);
|
||||
console.log('뒤쪽에 원소 4를 삽입한 후 deque = [' + deque.toArray() + ']');
|
||||
deque.pushFirst(1);
|
||||
console.log('앞쪽에 원소 1을 삽입한 후 deque = [' + deque.toArray() + ']');
|
||||
|
||||
/* 큐에서 원소 꺼내기 */
|
||||
const popLast = deque.popLast();
|
||||
console.log(
|
||||
'맨 뒤에서 꺼낸 원소 = ' +
|
||||
popLast +
|
||||
', 뒤쪽 원소를 꺼낸 후 deque = [' +
|
||||
deque.toArray() +
|
||||
']'
|
||||
);
|
||||
const popFirst = deque.popFirst();
|
||||
console.log(
|
||||
'맨 앞에서 꺼낸 원소 = ' +
|
||||
popFirst +
|
||||
', 앞쪽 원소를 꺼낸 후 deque = [' +
|
||||
deque.toArray() +
|
||||
']'
|
||||
);
|
||||
|
||||
/* 덱의 길이 반환 */
|
||||
const size = deque.size();
|
||||
console.log('덱 길이 size = ' + size);
|
||||
|
||||
/* 덱이 비어 있는지 판단 */
|
||||
const isEmpty = deque.isEmpty();
|
||||
console.log('덱이 비어 있는가 = ' + isEmpty);
|
||||
@@ -0,0 +1,106 @@
|
||||
/**
|
||||
* File: array_queue.js
|
||||
* Created Time: 2022-12-13
|
||||
* Author: S-N-O-R-L-A-X ([email protected])
|
||||
*/
|
||||
|
||||
/* 원형 배열로 구현한 큐 */
|
||||
class ArrayQueue {
|
||||
#nums; // 큐의 원소를 저장하는 배열
|
||||
#front = 0; // 맨 앞 원소를 가리키는 맨 앞 포인터
|
||||
#queSize = 0; // 큐 길이
|
||||
|
||||
constructor(capacity) {
|
||||
this.#nums = new Array(capacity);
|
||||
}
|
||||
|
||||
/* 큐의 용량 반환 */
|
||||
get capacity() {
|
||||
return this.#nums.length;
|
||||
}
|
||||
|
||||
/* 큐의 길이 반환 */
|
||||
get size() {
|
||||
return this.#queSize;
|
||||
}
|
||||
|
||||
/* 큐가 비어 있는지 판단 */
|
||||
isEmpty() {
|
||||
return this.#queSize === 0;
|
||||
}
|
||||
|
||||
/* 큐에 삽입 */
|
||||
push(num) {
|
||||
if (this.size === this.capacity) {
|
||||
console.log('큐가 가득 찼다');
|
||||
return;
|
||||
}
|
||||
// 맨 뒤 인덱스 + 1을 가리키는 맨 뒤 포인터를 계산한다
|
||||
// 나머지 연산을 통해 rear가 배열의 끝을 넘어가면 처음으로 돌아오게 한다
|
||||
const rear = (this.#front + this.size) % this.capacity;
|
||||
// num을 맨 뒤에 추가한다
|
||||
this.#nums[rear] = num;
|
||||
this.#queSize++;
|
||||
}
|
||||
|
||||
/* 큐에서 꺼내기 */
|
||||
pop() {
|
||||
const num = this.peek();
|
||||
// 맨 앞 포인터를 뒤로 한 칸 이동하고, 끝을 넘어가면 배열의 처음으로 돌아온다
|
||||
this.#front = (this.#front + 1) % this.capacity;
|
||||
this.#queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* 맨 앞 원소 접근 */
|
||||
peek() {
|
||||
if (this.isEmpty()) throw new Error('큐가 비어 있다');
|
||||
return this.#nums[this.#front];
|
||||
}
|
||||
|
||||
/* Array를 반환한다 */
|
||||
toArray() {
|
||||
// 유효한 길이 범위 안의 리스트 원소만 변환한다
|
||||
const arr = new Array(this.size);
|
||||
for (let i = 0, j = this.#front; i < this.size; i++, j++) {
|
||||
arr[i] = this.#nums[j % this.capacity];
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
}
|
||||
|
||||
/* 실행 코드 */
|
||||
/* 큐 초기화 */
|
||||
const capacity = 10;
|
||||
const queue = new ArrayQueue(capacity);
|
||||
|
||||
/* 큐에 원소 삽입 */
|
||||
queue.push(1);
|
||||
queue.push(3);
|
||||
queue.push(2);
|
||||
queue.push(5);
|
||||
queue.push(4);
|
||||
console.log('큐 queue =', queue.toArray());
|
||||
|
||||
/* 맨 앞 원소 접근 */
|
||||
const peek = queue.peek();
|
||||
console.log('맨 앞 원소 peek = ' + peek);
|
||||
|
||||
/* 큐에서 원소 꺼내기 */
|
||||
const pop = queue.pop();
|
||||
console.log('큐에서 꺼낸 원소 pop = ' + pop + ', 큐에서 꺼낸 후 queue =', queue.toArray());
|
||||
|
||||
/* 큐의 길이 반환 */
|
||||
const size = queue.size;
|
||||
console.log('큐 길이 size = ' + size);
|
||||
|
||||
/* 큐가 비어 있는지 판단 */
|
||||
const isEmpty = queue.isEmpty();
|
||||
console.log('큐가 비어 있는가 = ' + isEmpty);
|
||||
|
||||
/* 원형 배열 테스트 */
|
||||
for (let i = 0; i < 10; i++) {
|
||||
queue.push(i);
|
||||
queue.pop();
|
||||
console.log('반복 ' + i + ' 회에서 삽입 + 꺼내기를 수행한 후 queue =', queue.toArray());
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
/**
|
||||
* File: array_stack.js
|
||||
* Created Time: 2022-12-09
|
||||
* Author: S-N-O-R-L-A-X ([email protected])
|
||||
*/
|
||||
|
||||
/* 배열로 구현한 스택 */
|
||||
class ArrayStack {
|
||||
#stack;
|
||||
constructor() {
|
||||
this.#stack = [];
|
||||
}
|
||||
|
||||
/* 스택의 길이 반환 */
|
||||
get size() {
|
||||
return this.#stack.length;
|
||||
}
|
||||
|
||||
/* 스택이 비어 있는지 판단 */
|
||||
isEmpty() {
|
||||
return this.#stack.length === 0;
|
||||
}
|
||||
|
||||
/* 스택에 삽입 */
|
||||
push(num) {
|
||||
this.#stack.push(num);
|
||||
}
|
||||
|
||||
/* 스택에서 꺼내기 */
|
||||
pop() {
|
||||
if (this.isEmpty()) throw new Error('스택이 비어 있다');
|
||||
return this.#stack.pop();
|
||||
}
|
||||
|
||||
/* 스택 맨 위 원소 접근 */
|
||||
top() {
|
||||
if (this.isEmpty()) throw new Error('스택이 비어 있다');
|
||||
return this.#stack[this.#stack.length - 1];
|
||||
}
|
||||
|
||||
/* Array를 반환한다 */
|
||||
toArray() {
|
||||
return this.#stack;
|
||||
}
|
||||
}
|
||||
|
||||
/* 실행 코드 */
|
||||
/* 스택 초기화 */
|
||||
const stack = new ArrayStack();
|
||||
|
||||
/* 스택에 원소 삽입 */
|
||||
stack.push(1);
|
||||
stack.push(3);
|
||||
stack.push(2);
|
||||
stack.push(5);
|
||||
stack.push(4);
|
||||
console.log('스택 stack = ');
|
||||
console.log(stack.toArray());
|
||||
|
||||
/* 스택 맨 위 원소 접근 */
|
||||
const top = stack.top();
|
||||
console.log('스택의 맨 위 원소 top = ' + top);
|
||||
|
||||
/* 스택에서 원소 꺼내기 */
|
||||
const pop = stack.pop();
|
||||
console.log('스택에서 꺼낸 원소 pop = ' + pop + ', 스택에서 꺼낸 후 stack = ');
|
||||
console.log(stack.toArray());
|
||||
|
||||
/* 스택의 길이 반환 */
|
||||
const size = stack.size;
|
||||
console.log('스택의 길이 size = ' + size);
|
||||
|
||||
/* 비어 있는지 판단 */
|
||||
const isEmpty = stack.isEmpty();
|
||||
console.log('스택이 비어 있는가 = ' + isEmpty);
|
||||
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* File: deque.js
|
||||
* Created Time: 2023-01-17
|
||||
* Author: Zhuo Qinyue ([email protected])
|
||||
*/
|
||||
|
||||
/* 실행 코드 */
|
||||
/* 덱 초기화 */
|
||||
// JavaScript에는 내장 덱이 없으므로 Array를 덱으로 사용한다
|
||||
const deque = [];
|
||||
|
||||
/* 큐에 원소 삽입 */
|
||||
deque.push(2);
|
||||
deque.push(5);
|
||||
deque.push(4);
|
||||
// 배열이므로 unshift() 메서드의 시간 복잡도는 O(n)임에 유의한다
|
||||
deque.unshift(3);
|
||||
deque.unshift(1);
|
||||
console.log('덱 deque = ', deque);
|
||||
|
||||
/* 원소 접근 */
|
||||
const peekFirst = deque[0];
|
||||
console.log('맨 앞 원소 peekFirst = ' + peekFirst);
|
||||
const peekLast = deque[deque.length - 1];
|
||||
console.log('맨 뒤 원소 peekLast = ' + peekLast);
|
||||
|
||||
/* 큐에서 원소 꺼내기 */
|
||||
// 배열이므로 shift() 메서드의 시간 복잡도는 O(n)임에 유의한다
|
||||
const popFront = deque.shift();
|
||||
console.log(
|
||||
'맨 앞에서 꺼낸 원소 popFront = ' + popFront + ', 맨 앞에서 꺼낸 후 deque = ' + deque
|
||||
);
|
||||
const popBack = deque.pop();
|
||||
console.log(
|
||||
'뒤쪽에서 꺼낸 원소 popBack = ' + popBack + ', 맨 뒤에서 꺼낸 후 deque = ' + deque
|
||||
);
|
||||
|
||||
/* 덱의 길이 반환 */
|
||||
const size = deque.length;
|
||||
console.log('덱 길이 size = ' + size);
|
||||
|
||||
/* 덱이 비어 있는지 판단 */
|
||||
const isEmpty = size === 0;
|
||||
console.log('덱이 비어 있는가 = ' + isEmpty);
|
||||
@@ -0,0 +1,177 @@
|
||||
/**
|
||||
* File: linkedlist_deque.js
|
||||
* Created Time: 2023-02-04
|
||||
* Author: Zhuo Qinyue ([email protected])
|
||||
*/
|
||||
|
||||
/* 이중 연결 리스트 노드 */
|
||||
class ListNode {
|
||||
prev; // 이전 노드 참조(포인터)
|
||||
next; // 다음 노드 참조(포인터)
|
||||
val; // 노드 값
|
||||
|
||||
constructor(val) {
|
||||
this.val = val;
|
||||
this.next = null;
|
||||
this.prev = null;
|
||||
}
|
||||
}
|
||||
|
||||
/* 이중 연결 리스트로 구현한 덱 */
|
||||
class LinkedListDeque {
|
||||
#front; // 머리 노드 front
|
||||
#rear; // 꼬리 노드 rear
|
||||
#queSize; // 덱의 길이
|
||||
|
||||
constructor() {
|
||||
this.#front = null;
|
||||
this.#rear = null;
|
||||
this.#queSize = 0;
|
||||
}
|
||||
|
||||
/* 맨 뒤에 삽입하는 연산 */
|
||||
pushLast(val) {
|
||||
const node = new ListNode(val);
|
||||
// 연결 리스트가 비어 있으면 front와 rear가 모두 node를 가리키게 한다
|
||||
if (this.#queSize === 0) {
|
||||
this.#front = node;
|
||||
this.#rear = node;
|
||||
} else {
|
||||
// node를 연결 리스트의 맨 뒤에 추가한다
|
||||
this.#rear.next = node;
|
||||
node.prev = this.#rear;
|
||||
this.#rear = node; // 꼬리 노드를 갱신한다
|
||||
}
|
||||
this.#queSize++;
|
||||
}
|
||||
|
||||
/* 맨 앞에 삽입하는 연산 */
|
||||
pushFirst(val) {
|
||||
const node = new ListNode(val);
|
||||
// 연결 리스트가 비어 있으면 front와 rear가 모두 node를 가리키게 한다
|
||||
if (this.#queSize === 0) {
|
||||
this.#front = node;
|
||||
this.#rear = node;
|
||||
} else {
|
||||
// node를 연결 리스트의 맨 앞에 추가한다
|
||||
this.#front.prev = node;
|
||||
node.next = this.#front;
|
||||
this.#front = node; // 머리 노드를 갱신한다
|
||||
}
|
||||
this.#queSize++;
|
||||
}
|
||||
|
||||
/* 맨 뒤에서 꺼내는 연산 */
|
||||
popLast() {
|
||||
if (this.#queSize === 0) {
|
||||
return null;
|
||||
}
|
||||
const value = this.#rear.val; // 꼬리 노드의 값을 저장한다
|
||||
// 큐의 길이가 1이면 머리와 꼬리가 같은 노드를 가리킨다
|
||||
if (this.#queSize === 1) {
|
||||
this.#front = null;
|
||||
this.#rear = null;
|
||||
}
|
||||
// 꼬리 노드를 삭제한다
|
||||
else {
|
||||
let temp = this.#rear.prev;
|
||||
temp.next = null;
|
||||
this.#rear.prev = null;
|
||||
this.#rear = temp; // 꼬리 노드를 갱신한다
|
||||
}
|
||||
this.#queSize--;
|
||||
return value;
|
||||
}
|
||||
|
||||
/* 맨 앞에서 꺼내는 연산 */
|
||||
popFirst() {
|
||||
if (this.#queSize === 0) {
|
||||
return null;
|
||||
}
|
||||
const value = this.#front.val; // 머리 노드의 값을 저장한다
|
||||
// 큐의 길이가 1이면 머리와 꼬리가 같은 노드를 가리킨다
|
||||
if (this.#queSize === 1) {
|
||||
this.#front = null;
|
||||
this.#rear = null;
|
||||
}
|
||||
// 머리 노드를 삭제한다
|
||||
else {
|
||||
let temp = this.#front.next;
|
||||
temp.prev = null;
|
||||
this.#front.next = null;
|
||||
this.#front = temp; // 머리 노드를 갱신한다
|
||||
}
|
||||
this.#queSize--;
|
||||
return value;
|
||||
}
|
||||
|
||||
/* 맨 뒤 원소 접근 */
|
||||
peekLast() {
|
||||
return this.#queSize === 0 ? null : this.#rear.val;
|
||||
}
|
||||
|
||||
/* 맨 앞 원소 접근 */
|
||||
peekFirst() {
|
||||
return this.#queSize === 0 ? null : this.#front.val;
|
||||
}
|
||||
|
||||
/* 덱의 길이 반환 */
|
||||
size() {
|
||||
return this.#queSize;
|
||||
}
|
||||
|
||||
/* 덱이 비어 있는지 판단 */
|
||||
isEmpty() {
|
||||
return this.#queSize === 0;
|
||||
}
|
||||
|
||||
/* 덱을 출력한다 */
|
||||
print() {
|
||||
const arr = [];
|
||||
let temp = this.#front;
|
||||
while (temp !== null) {
|
||||
arr.push(temp.val);
|
||||
temp = temp.next;
|
||||
}
|
||||
console.log('[' + arr.join(', ') + ']');
|
||||
}
|
||||
}
|
||||
|
||||
/* 실행 코드 */
|
||||
/* 덱 초기화 */
|
||||
const linkedListDeque = new LinkedListDeque();
|
||||
linkedListDeque.pushLast(3);
|
||||
linkedListDeque.pushLast(2);
|
||||
linkedListDeque.pushLast(5);
|
||||
console.log('덱 linkedListDeque = ');
|
||||
linkedListDeque.print();
|
||||
|
||||
/* 원소 접근 */
|
||||
const peekFirst = linkedListDeque.peekFirst();
|
||||
console.log('맨 앞 원소 peekFirst = ' + peekFirst);
|
||||
const peekLast = linkedListDeque.peekLast();
|
||||
console.log('맨 뒤 원소 peekLast = ' + peekLast);
|
||||
|
||||
/* 큐에 원소 삽입 */
|
||||
linkedListDeque.pushLast(4);
|
||||
console.log('뒤쪽에 원소 4를 삽입한 후 linkedListDeque = ');
|
||||
linkedListDeque.print();
|
||||
linkedListDeque.pushFirst(1);
|
||||
console.log('앞쪽에 원소 1을 삽입한 후 linkedListDeque = ');
|
||||
linkedListDeque.print();
|
||||
|
||||
/* 큐에서 원소 꺼내기 */
|
||||
const popLast = linkedListDeque.popLast();
|
||||
console.log('맨 뒤에서 꺼낸 원소 = ' + popLast + ', 뒤쪽 원소를 꺼낸 후 linkedListDeque = ');
|
||||
linkedListDeque.print();
|
||||
const popFirst = linkedListDeque.popFirst();
|
||||
console.log('맨 앞에서 꺼낸 원소 = ' + popFirst + ', 앞쪽 원소를 꺼낸 후 linkedListDeque = ');
|
||||
linkedListDeque.print();
|
||||
|
||||
/* 덱의 길이 반환 */
|
||||
const size = linkedListDeque.size();
|
||||
console.log('덱 길이 size = ' + size);
|
||||
|
||||
/* 덱이 비어 있는지 판단 */
|
||||
const isEmpty = linkedListDeque.isEmpty();
|
||||
console.log('덱이 비어 있는가 = ' + isEmpty);
|
||||
@@ -0,0 +1,99 @@
|
||||
/**
|
||||
* File: linkedlist_queue.js
|
||||
* Created Time: 2022-12-20
|
||||
* Author: S-N-O-R-L-A-X ([email protected])
|
||||
*/
|
||||
|
||||
const { ListNode } = require('../modules/ListNode');
|
||||
|
||||
/* 연결 리스트로 구현한 큐 */
|
||||
class LinkedListQueue {
|
||||
#front; // 머리 노드 #front
|
||||
#rear; // 꼬리 노드 #rear
|
||||
#queSize = 0;
|
||||
|
||||
constructor() {
|
||||
this.#front = null;
|
||||
this.#rear = null;
|
||||
}
|
||||
|
||||
/* 큐의 길이 반환 */
|
||||
get size() {
|
||||
return this.#queSize;
|
||||
}
|
||||
|
||||
/* 큐가 비어 있는지 판단 */
|
||||
isEmpty() {
|
||||
return this.size === 0;
|
||||
}
|
||||
|
||||
/* 큐에 삽입 */
|
||||
push(num) {
|
||||
// 꼬리 노드 뒤에 num을 추가한다
|
||||
const node = new ListNode(num);
|
||||
// 큐가 비어 있으면 머리와 꼬리 노드가 모두 해당 노드를 가리키게 한다
|
||||
if (!this.#front) {
|
||||
this.#front = node;
|
||||
this.#rear = node;
|
||||
// 큐가 비어 있지 않으면 해당 노드를 꼬리 노드 뒤에 추가한다
|
||||
} else {
|
||||
this.#rear.next = node;
|
||||
this.#rear = node;
|
||||
}
|
||||
this.#queSize++;
|
||||
}
|
||||
|
||||
/* 큐에서 꺼내기 */
|
||||
pop() {
|
||||
const num = this.peek();
|
||||
// 머리 노드를 삭제한다
|
||||
this.#front = this.#front.next;
|
||||
this.#queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* 맨 앞 원소 접근 */
|
||||
peek() {
|
||||
if (this.size === 0) throw new Error('큐가 비어 있다');
|
||||
return this.#front.val;
|
||||
}
|
||||
|
||||
/* 연결 리스트를 Array로 변환하여 반환 */
|
||||
toArray() {
|
||||
let node = this.#front;
|
||||
const res = new Array(this.size);
|
||||
for (let i = 0; i < res.length; i++) {
|
||||
res[i] = node.val;
|
||||
node = node.next;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
/* 실행 코드 */
|
||||
/* 큐 초기화 */
|
||||
const queue = new LinkedListQueue();
|
||||
|
||||
/* 큐에 원소 삽입 */
|
||||
queue.push(1);
|
||||
queue.push(3);
|
||||
queue.push(2);
|
||||
queue.push(5);
|
||||
queue.push(4);
|
||||
console.log('큐 queue = ' + queue.toArray());
|
||||
|
||||
/* 맨 앞 원소 접근 */
|
||||
const peek = queue.peek();
|
||||
console.log('맨 앞 원소 peek = ' + peek);
|
||||
|
||||
/* 큐에서 원소 꺼내기 */
|
||||
const pop = queue.pop();
|
||||
console.log('큐에서 꺼낸 원소 pop = ' + pop + ', 큐에서 꺼낸 후 queue = ' + queue.toArray());
|
||||
|
||||
/* 큐의 길이 반환 */
|
||||
const size = queue.size;
|
||||
console.log('큐 길이 size = ' + size);
|
||||
|
||||
/* 큐가 비어 있는지 판단 */
|
||||
const isEmpty = queue.isEmpty();
|
||||
console.log('큐가 비어 있는가 = ' + isEmpty);
|
||||
@@ -0,0 +1,88 @@
|
||||
/**
|
||||
* File: linkedlist_stack.js
|
||||
* Created Time: 2022-12-22
|
||||
* Author: S-N-O-R-L-A-X ([email protected])
|
||||
*/
|
||||
|
||||
const { ListNode } = require('../modules/ListNode');
|
||||
|
||||
/* 연결 리스트로 구현한 스택 */
|
||||
class LinkedListStack {
|
||||
#stackPeek; // 머리 노드를 스택의 맨 위로 사용한다
|
||||
#stkSize = 0; // 스택의 길이
|
||||
|
||||
constructor() {
|
||||
this.#stackPeek = null;
|
||||
}
|
||||
|
||||
/* 스택의 길이 반환 */
|
||||
get size() {
|
||||
return this.#stkSize;
|
||||
}
|
||||
|
||||
/* 스택이 비어 있는지 판단 */
|
||||
isEmpty() {
|
||||
return this.size === 0;
|
||||
}
|
||||
|
||||
/* 스택에 삽입 */
|
||||
push(num) {
|
||||
const node = new ListNode(num);
|
||||
node.next = this.#stackPeek;
|
||||
this.#stackPeek = node;
|
||||
this.#stkSize++;
|
||||
}
|
||||
|
||||
/* 스택에서 꺼내기 */
|
||||
pop() {
|
||||
const num = this.peek();
|
||||
this.#stackPeek = this.#stackPeek.next;
|
||||
this.#stkSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* 스택 맨 위 원소 접근 */
|
||||
peek() {
|
||||
if (!this.#stackPeek) throw new Error('스택이 비어 있다');
|
||||
return this.#stackPeek.val;
|
||||
}
|
||||
|
||||
/* 연결 리스트를 Array로 변환하여 반환 */
|
||||
toArray() {
|
||||
let node = this.#stackPeek;
|
||||
const res = new Array(this.size);
|
||||
for (let i = res.length - 1; i >= 0; i--) {
|
||||
res[i] = node.val;
|
||||
node = node.next;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
/* 실행 코드 */
|
||||
/* 스택 초기화 */
|
||||
const stack = new LinkedListStack();
|
||||
|
||||
/* 스택에 원소 삽입 */
|
||||
stack.push(1);
|
||||
stack.push(3);
|
||||
stack.push(2);
|
||||
stack.push(5);
|
||||
stack.push(4);
|
||||
console.log('스택 stack = ' + stack.toArray());
|
||||
|
||||
/* 스택 맨 위 원소 접근 */
|
||||
const peek = stack.peek();
|
||||
console.log('스택 맨 위 원소 peek = ' + peek);
|
||||
|
||||
/* 스택에서 원소 꺼내기 */
|
||||
const pop = stack.pop();
|
||||
console.log('스택에서 꺼낸 원소 pop = ' + pop + ', 스택에서 꺼낸 후 stack = ' + stack.toArray());
|
||||
|
||||
/* 스택의 길이 반환 */
|
||||
const size = stack.size;
|
||||
console.log('스택의 길이 size = ' + size);
|
||||
|
||||
/* 비어 있는지 판단 */
|
||||
const isEmpty = stack.isEmpty();
|
||||
console.log('스택이 비어 있는가 = ' + isEmpty);
|
||||
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* File: queue.js
|
||||
* Created Time: 2022-12-05
|
||||
* Author: S-N-O-R-L-A-X ([email protected])
|
||||
*/
|
||||
|
||||
/* 실행 코드 */
|
||||
/* 큐 초기화 */
|
||||
// JavaScript에는 내장 큐가 없으므로 Array를 큐로 사용할 수 있다
|
||||
const queue = [];
|
||||
|
||||
/* 큐에 원소 삽입 */
|
||||
queue.push(1);
|
||||
queue.push(3);
|
||||
queue.push(2);
|
||||
queue.push(5);
|
||||
queue.push(4);
|
||||
console.log('큐 queue =', queue);
|
||||
|
||||
/* 맨 앞 원소 접근 */
|
||||
const peek = queue[0];
|
||||
console.log('맨 앞 원소 peek =', peek);
|
||||
|
||||
/* 큐에서 원소 꺼내기 */
|
||||
// 내부 자료 구조가 배열이므로 shift() 메서드의 시간 복잡도는 O(n)이다
|
||||
const pop = queue.shift();
|
||||
console.log('큐에서 꺼낸 원소 pop =', pop, ', 큐에서 꺼낸 후 queue = ', queue);
|
||||
|
||||
/* 큐의 길이 반환 */
|
||||
const size = queue.length;
|
||||
console.log('큐 길이 size =', size);
|
||||
|
||||
/* 큐가 비어 있는지 판단 */
|
||||
const isEmpty = queue.length === 0;
|
||||
console.log('큐가 비어 있는가 = ', isEmpty);
|
||||
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* File: stack.js
|
||||
* Created Time: 2022-12-04
|
||||
* Author: S-N-O-R-L-A-X ([email protected])
|
||||
*/
|
||||
|
||||
/* 실행 코드 */
|
||||
/* 스택 초기화 */
|
||||
// JavaScript에는 내장 스택 클래스가 없으므로 Array를 스택으로 사용할 수 있다
|
||||
const stack = [];
|
||||
|
||||
/* 스택에 원소 삽입 */
|
||||
stack.push(1);
|
||||
stack.push(3);
|
||||
stack.push(2);
|
||||
stack.push(5);
|
||||
stack.push(4);
|
||||
console.log('스택 stack =', stack);
|
||||
|
||||
/* 스택 맨 위 원소 접근 */
|
||||
const peek = stack[stack.length - 1];
|
||||
console.log('스택 맨 위 원소 peek =', peek);
|
||||
|
||||
/* 스택에서 원소 꺼내기 */
|
||||
const pop = stack.pop();
|
||||
console.log('스택에서 꺼낸 원소 pop =', pop);
|
||||
console.log('스택에서 꺼낸 후 stack =', stack);
|
||||
|
||||
/* 스택의 길이 반환 */
|
||||
const size = stack.length;
|
||||
console.log('스택의 길이 size =', size);
|
||||
|
||||
/* 비어 있는지 판단 */
|
||||
const isEmpty = stack.length === 0;
|
||||
console.log('스택이 비어 있는가 =', isEmpty);
|
||||
Reference in New Issue
Block a user