자료구조 - 스택(Stack)

조성주·2023년 3월 25일
0

자료구조

목록 보기
4/12
post-thumbnail

❓ 스택(Stack)

  • 나중에 넣은 데이터가 먼저 나오는 LIFO(Last In First Out) 기반의 선형 자료 구조이다.
// Stack() : 생성자 함수
function Stack(array) {
  this.array = array ? array : [];
}


✏️ 구현 메서드(Method)

📗 getBuffer() : 객체 내 데이터 셋 반환

Stack.prototype.getBuffer = function () {
  return this.array.slice(); // 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
};
profile
프론트엔드 개발자가 되기 위한 기록

0개의 댓글