참조

https://velog.io/@blackb0x/%EC%9E%90%EB%B0%94%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8%EC%9D%98-%EC%9E%90%EB%A3%8C%EA%B5%AC%EC%A1%B0 


자바스크립트 참조타입에는 object, array, function 이 있다. 이 외의 자료구조는 만들어야한다.

 

array(배열)

 

object(객체)

객체는 Hash Table의 종류 중 하나이다.

배열은 Key가 오직 숫자만 가능한 것에 비해 Hash Table에서는 문자열 또한 Key가 될 수 있다.

 

stack

LIFO(Last In, First Out)

스택은 브라우저 히스토리(이전 페이지, 다음 페이지), ctrl+z로 이전 작업을 취소하는 등의 동작에 쓰인다.

class Stack {
  constructor() {
    this.arr = [];
    this.index = 0;
  }
  push(item) {
    this.arr[this.index++] = item;
  }
  pop() {
    if (this.index <= 0) return null;
    const result = this.arr[--this.index];
    return result;
  }
}

 

queue

FIFO(First In, First Out)

큐는 레스토랑 앱, 예매 앱, 우버 앱 등에서 쓰인다.

class Queue {
  constructor() {
    this.arr = [];
    this.head = 0;
    this.tail = 0;
  }
  enqueue(item) {
    this.arr[this.tail++] = item;
  }
  dequeue() {
    if (this.head >= this.tail) {
      return null;
    } else {
      const result = this.arr[this.head++];
      return result;
    }
  }
}

 

graph

  • linked list
  • tree

'개발 > Javascript(Typescript)' 카테고리의 다른 글

nodejs next  (0) 2023.10.25
promise, 동기, 비동기  (0) 2023.10.11
for문을 동기적으로 처리하기  (1) 2023.10.10
자바스크립트 예외 처리(Exception Handling)  (0) 2023.09.07
bcrypt로 비밀번호 암호화하기  (0) 2023.08.31

+ Recent posts