push : 데이터 삽입
pop : 데이터 추출 + 삭제
peek: head 데이터 확인(출력하면 가장 먼저 나올 데이터)
isEmpty: 비어있는지 확인
class Stack {
constructor() {
this._arr = [];
}
push(item) {
this._arr.push(item);
}
pop() {
return this._arr.pop();
}
peek() {
return this._arr[this._arr.length - 1];
}
isEmpty() {
return this._arr.length === 0 ? true : false;
}
}
import LinkedList from '../linked-list/LinkedList';
// 기존에 제작한 LinkedList import
export default class Stack {
constructor() {
this.linkedList = new LinkedList();
}
push(value) {
this.linkedList.prepend(value);
}
pop() {
const removedHead = this.linkedList.deleteHead();
return removedHead ? removedHead.value : null;
}
isEmpty() {
return !this.linkedList.head;
}
peek() {
if (this.isEmpty()) {
return null;
}
return this.linkedList.head.value;
}
}
Github | tech-interview-for-developer
Github | Interview_Question_for_Beginner
Github | javascript-algorithms | trekhleb
helloworldjavascript
Photo by Alain Pham on Unsplash