Jeonghwan's Blog
스택, 한 번에 쌓아 올리는 자료구조 이해하기

스택, 한 번에 쌓아 올리는 자료구조 이해하기

스택이란?

스택(Stack)은 LIFO(Last In First Out)이라는 개념을 가진 선형 자료구조입니다.

stack

JavaScript로 구현하기

배열(Array)로 구현

const stack = [];
 
// Push
stack.push(1);
stack.push(2);
stack.push(3);
 
console.log(stack); // [1, 2, 3]
 
// Pop
stack.pop();
 
console.log(stack); // [1, 2]

연결 리스트(Linked List)로 구현

class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}
 
class Stack {
  constructor() {
    this.top = null;
    this.size = 0;
  }
 
  push(value) {
    const node = new Node(value);
 
    node.next = this.top;
    this.top = node;
    this.size += 1;
  }
 
  pop() {
    const value = this.top.value;
 
    this.top = this.top.next;
    this.size -= 1;
 
    return value;
  }
 
  size() {
    return this.size;
  }
}
 
const stack = new Stack();

참고 자료