■ Front-End/- JavaScript & TypeScript
-
[자료구조] Javascript로 Queue 구현하기■ Front-End/- JavaScript & TypeScript 2019. 5. 23. 00:21
Javascript로 Queue를 구현한 코드이다. 이 코드도 Stack과 마찬가지로 Javascript에서 기본적으로 제공하는 push(), pop() 메서드를 사용하지 않고 작성했다. class Queue { constructor() { this.bucket = []; } isEmpty() { return this.bucket.length == 0; } enqueue(val) { this.bucket[this.bucket.length] = val; } dequeue() { if(this.bucket.length == 0) { return -1; } let val = this.bucket[0]; this.bucket = this.bucket.slice(1, this.bucket.length); cons..
-
[자료구조] Javascript로 Stack 구현하기■ Front-End/- JavaScript & TypeScript 2019. 5. 23. 00:07
Javascript에서 제공하고 있는 push(), pop()을 사용하지 않고 Stack Class를 만들어 구현하는 코드이다. class Stack { constructor() { this.top = -1; this.bucket = []; } isEmpty() { return this.bucket.length == 0; } push(val) { this.bucket[++this.top] = val; } pop() { if(this.top < 0) { return -1; } else { let popVal = this.bucket[this.top]; this.bucket = this.bucket.slice(0, this.top); this.top--; return popVal; } } peek() { r..