\(@^0^@)/

[알고리즘] 스택(Stack)과 구현 메서드 본문

알고리즘

[알고리즘] 스택(Stack)과 구현 메서드

minjuuu 2022. 5. 9. 16:12
728x90

스택 (Stack)

  • 나중에 넣은 데이터가 먼저 나오는 LIFO(Last In First Out) 기반의 선형 자료 구조
  • 구현 메서드 (method)
    • 데이터 전체 획득 / 비어 있는지 확인 : Stack.getBuffer(), Stack.isEmpty()
    • 추가 / 삭제 / 마지막 데이터 조회 / 크기 확인 : Stack.push(), Stack.pop(), Stack.peak(), Stack.size()
    • 데이터 위치 / 존재 여부 확인 : Stack.indexOf(), Stack.includes()


 

  • Stack() : 생성자 함수로 초기 데이터 설정
    getBuffer() : 객체 내 데이터 셋 반환
    isEmpty() : 객체 내 데이터 존재 여부 파악
// Stack(): 생성자 함수로 초기 데이터 설정
function Stack(array) {
	this.array = array ? array : [];
}

// getBuffer(): 객체 내 데이터 셋 반환
Stack.prototype.getBuffer = function () {
	return this.array.slice();
};

// isEmpty(): 객체 내 데이터 존배 여부 파악
Stack.prototype.isEmpty = function () {
	return this.array.length == 0;
};

let stack = new Stack([1, 2, 3]);
console.log(stack); // Stack { array: [ 1, 2, 3 ] }

let data = stack.getBuffer();
console.log(data === stack.array); // false
console.log(data); // [1, 2, 3]

console.log(stack.isEmpty()); // false

 

  • push() : 데이터 추가
    pop() : 데이터 삭제
    peek() : 가장 끝 데이터 반환
    size() : 스택 내 데이터 개수 확인
// 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;
};

let stack = new Stack([1, 2, 3]);

console.log(stack); // Stack { array: [ 1, 2, 3 ] }
stack.push(4);
console.log(stack); // Stack { array: [ 1, 2, 3, 4 ] }

console.log(stack.pop()); // 4
console.log(stack.pop()); // 3
console.log(stack.peek()); // 2
console.log(stack.size()); // 2

 

  • indexOf() : 데이터 위치 값 조회
    includes() : 데이터 존재 여부 확인
// indexOf(): 데이터 위치 값 조회
Stack.prototype.indexOf = function (element, position = 0) {
	/* case 1 */
	// return this.array.indexOf(element, position);

	/* case 2 */
	for (let i = position; i < element; i++) {
		if (element == this.array[i]) return i;
	}
	return -1;
};

// includes() : 데이터 존재 여부 확인
Stack.prototype.includes = function (element, position = 0) {
	/* case 1 */
	return this.array.includes(element, position);

	/* case 2 */
	for (let i = position; i < element; i++) {
		if (element == this.array[i]) return true;
	}
	return false;
};

let stack = new Stack([1, 2, 3]);

console.log(stack.indexOf(1)); // 0
console.log(stack.indexOf(1, 2)); // -1
console.log(stack.includes(1)); // true
console.log(stack.includes(1, 2)); // false

 

728x90