일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 |
- Docker
- review
- 쿠버네티스
- 시장
- 투자
- strongloop
- game
- angular
- 게임
- PC
- 보안
- 이더리움
- 비트코인
- 거래
- kubernetes
- threejs
- 주식
- 탈중앙화
- Three
- Linux
- ps4
- 젤다 왕눈
- 리뷰
- Games
- 암호화폐
- 블록체인
- 스마트 계약
- 스마트 컨트랙트
- loopback
- 부동산
- Today
- Total
BaeBox
Stack(스택) with JavaScript 본문
* node 에서는 기본적으로 class를 사용하지 못하므로 babel을 사용해야 한다. 적용법은 최하단 링크에 있다. 귀찮으면 아래 링크의 git을 clone을 하시면 된다.
Stack : 쌓아 올리는 형태의 자료구조. 먼저 넣을수록 가장 나중에 꺼내게 된다. (LIFO : Last In First Out)
컴퓨터 내부 동작에 많이 사용되는 자료구조이다(함수 호출 등).
class Stack {
stackArray;
constructor() {
this.stackArray = [];
}
setArray = (arr) => this.stackArray = arr;
getArray = () => this.stackArray;
size = () => this.stackArray.length;
top = () => this.stackArray[this.stackArray.length - 1];
pop = () => {
this.stackArray = this.stackArray.slice(0, this.stackArray.length - 1);
return this.stackArray[this.stackArray.length - 1];
// this.stackArray.pop();
};
push = (arg) => {
const newArray = new Array(this.stackArray.length + 1);
this.stackArray.map((each, index) => newArray[index] = each);
newArray[newArray.length - 1] = arg;
this.stackArray = newArray;
// this.stackArray.push(arg);
};
}
const stack = new Stack();
// stack.setArray([10, 9, 8, 7, 6]);
stack.push(5);
stack.push(4);
stack.push(3);
stack.push(2);
stack.push(1);
console.log(stack.getArray());
console.log(stack.top());
console.log(stack.size());
pop 과 push 의 경우에 주석친 코드로 단박에 해결이 가능한데, stackTrace 찍어보니 내부에서 일일히 객체를 생성하는 것 같아서 날것 그대로 코드를 짜 보았다.
아래의 내 깃허브에서 소스를 볼 수 있다.
https://github.com/iamdap91/basic-data-structure
iamdap91/basic-data-structure
basic-data-structure. Contribute to iamdap91/basic-data-structure development by creating an account on GitHub.
github.com
http://jeonghwan-kim.github.io/2016/07/19/babel.html
Babel로 ES6 코드 사용하기
바벨(Babel)을 처음 들은것이 작년이다.ES6 초안이 나온 상황에서 미리 사용해 보고 싶은 이들위해 ES5용 코드로 변환해 주는 기능이다.나는 머지않아 ES6 스펙이 확정될 것이고 더불어 브라우져와 노드에서는 신속히 지원해 줄 것이라 생각했다.그래서 굳이 바벨이라는 툴을 학습할...
jeonghwan-kim.github.io
'개발 관련' 카테고리의 다른 글
LinkedList(연결 리스트) with JavaScript (0) | 2020.04.19 |
---|---|
Queue(큐) with JavaScript (0) | 2020.04.19 |
Angular Custom Webpack (0) | 2020.04.12 |
Module Bundler (모듈 번들러) (0) | 2020.04.11 |
Mocha(모카) - JavaScript Test Framework (0) | 2020.04.11 |