스택은 LIFO 원칙을 따르는 선형 자료 구조이다.
LIFO는 후입선출인데, 가장 나중에 들어간 데이터가 가장 먼저 나오는 형식이다.
스택은 ADT이다. 추상 자료형이다.
ADT는 논리적인 특성을 정의하지만, 직접적인 구현은 추상화하는 것.
스택은 배열처럼 데이터를 List 형태로 저장.
push() => 삽입
pop() => 삭제
peek() => 읽기
class Stack {
constructor() {
this._items = [];
}
push(item) {
this._itmes.push(item);
}
pop() {
this._items.pop();
}
peek() {
return this._items.at(-1);
}
}
const s = new Stack();
s.push(10);
s.push(20);
s.push(30);
s.push(40);
console.log(s) // [10,20,30,40]
console.log(s.peek); // 40
s.pop();
s.pop();
console.log(s) // [10,20];