
"먼저 들어간 것이 나중에 나온다!"
스택은 나중에 들어간 것이 먼저 나오는 구조로, 후입선출 또는 LIFO(Last-in, First-Out)의 자료구조이다.
스택에는 자료를 넣고, 꺼내고, 들여다 보는 연산으로 구성되며, 각각 push, pop, peek이라 한다. 또한, 스택으로 구현한 대표적인 프로그램이 '계산기'이다.
Node구성
class Node {
  constructor(data) {
    this.data = data;
    this.next = null;
  }
}
class Stack {
  constructor() {
    this.top = null;
    this.count = 0;
  }
  push(data) {
    let node = new Node(data);
    node.next = this.top; // 삽입된 node의 next(아래가) 기존의 top이된다.
    this.top = node;  // node가 새로운 top이된다.
    return ++this.count;
  }
  pop() {
    if (!this.top) {
      return false;
    }
    let data = this.top.data;
    this.top = this.top.next;  
    this.count--;
    return data;
  }
  peek() {
    console.log(this.top.data);
  }
}
let stack = new Stack();
console.log(stack)
stack.push(100)
stack.push(200)
stack.push(300)
stack.pop();
//stack.peek();
console.log(stack)
감사히 잘봤습니다!