❓ 스택(Stack)
- 나중에 넣은 데이터가 먼저 나오는 LIFO(Last In First Out) 기반의 선형 자료 구조이다.
function Stack(array) {
this.array = array ? array : [];
}
✏️ 구현 메서드(Method)
📗 getBuffer() : 객체 내 데이터 셋 반환
Stack.prototype.getBuffer = function () {
return this.array.slice();
}
📗 isEmpty() : 객체 내 데이터 있는지 없는지 반환
Stack.prototype.isEmpty = function () {
return this.array.length === 0;
}
📗 push() : 데이터 추가
Stack.prototype.push = function (element) {
return this.array.push(element);
}
📗 pop() : 데이터 삭제
Stack.prototype.pop = function () {
return this.array.pop();
}
📗 peek() : 가장 끝 데이터 반환
Stack.prototype.peek = function () {
return this.array[this.array.length - 1];
}
📗 size() : 스택 내 데이터 개수 확인
Stack.prototype.size = function () {
return this.array.length;
}
📗 indexOf() : 데이터 위치 값 조회
Stack.prototype.indexOf = function (element, position = 0){
for (let i = position; i < this.array.length; i++){
if (this.array[i] === element) return i;
}
return -1;
}
📗 includes() : 데이터 존재 여부 확인
Stack.prototype.includes = function (element) {
for(let i = 0; i < this.array.length; i++){
if (this.array[i] === element) return true;
}
return false
};