[자료구조] 스택-큐

2022. 11. 28. 15:24·개발/자료구조
728x90
반응형

1. 스택과 큐

스택 : 후입선출 - 나중에 들어온것이 먼저 나간다.

큐 : 선입선출 - 먼저 들어온것이 먼저 나간다.

배열을 이용하여 구현한다면 스택은 배열의 기본 메소드 push와 pop을 이용하면 시간 복잡도 O(1)로 처리할 수 있지만

큐는 push, shift 또는 unshift, pop 을 이용하기 때문에 시간복잡도 O(n)이다.

자료구조를 직접 만들어 사용한다면 큐의 시간복잡도를 O(1)로 낮출수 있다.

2. 스택

(1) 클래스 및 노드

class Node {
  constructor(val) {
    this.val = val;
    this.next = null;
  }
}

class Stack {
  constructor() {
    this.first = null;
    this.last = null;
    this.size = 0;
  }
}

(2) push

push(val) {
    let newNode = new Node(val);
    if (!this.first) {
      this.first = newNode;
      this.last = newNode;
    } else {
      let temp = this.first;
      this.first = newNode;
      this.first.next = temp;
    }
    return ++this.size;
  }

(3) pop

pop() {
    if (!this.first) return null;
    let temp = this.first;
    if (this.first === this.last) {
      this.last = null;
    }
    this.first = this.first.next;
    this.size--;
    return temp.val;
  }

3. 스택의 빅O

insertion(삽입) removal(제거) searching(탐색) access(접근)
O(1) O(1) O(n) O(n)

4. 큐

(1) 큐의 사용처 예시

  - 게임 로딩대기시 먼저 로딩에 들어간사람 들여보내주기

  - 여러개 파일 다운로드시 먼저 다운로드 클릭한것부터 다운로드 하기

  - 프린트할때 먼저 출력한것부터 먼저 나오게 하기

(2) 클래스 및 노드

class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

class Queue {
  constructor() {
    this.first = null;
    this.last = null;
    this.size = 0;
  }
}

(3) enqueue

enqueue(val) {
    let newNode = new Node(val);
    if (!this.first) {
      this.first = newNode;
      this.last = newNode;
    } else {
      this.last.next = newNode;
      this.last = newNode;
    }
    return ++this.size;
  }

(4) dequeue

dequeue() {
    if (!this.first) return null;

    let temp = this.first;
    if (this.first === this.last) {
      this.last = null;
    }
    this.first = this.first.next;
    this.size--;
    return temp.value;
  }

5. 큐의 빅O

insertion(삽입) removal(제거) searching(탐색) access(접근)
O(1) O(1) O(n) O(n)

 

728x90
반응형

'개발 > 자료구조' 카테고리의 다른 글

[자료구조] 이진힙 - BinaryHeap  (0) 2022.12.02
[자료구조] 너비 우선 탐색(BFS), 깊이 우선 탐색(DFS)  (0) 2022.11.28
[자료구조] 이진트리  (0) 2022.11.28
[자료구조] 양방향 연결리스트  (0) 2022.11.22
[자료구조] 단방향 연결리스트  (1) 2022.11.22
'개발/자료구조' 카테고리의 다른 글
  • [자료구조] 너비 우선 탐색(BFS), 깊이 우선 탐색(DFS)
  • [자료구조] 이진트리
  • [자료구조] 양방향 연결리스트
  • [자료구조] 단방향 연결리스트
TeTedo.
TeTedo.
  • TeTedo.
    TeTedo 개발 일기
    TeTedo.
  • 전체
    오늘
    어제
    • 분류 전체보기 (319)
      • 개발 (274)
        • Article (4)
        • 정리 (21)
        • Spring Boot (17)
        • JPA (2)
        • JAVA (6)
        • Database (4)
        • 자료구조 (11)
        • 알고리즘 (32)
        • React (20)
        • Docker (10)
        • node.js (18)
        • Devops (11)
        • Linux (4)
        • TypeScript (3)
        • Go (10)
        • HyperLedger (4)
        • BlockChain (43)
        • html, css, js (48)
        • CS (3)
        • AWS (3)
      • 모아두고 나중에 쓰기 (3)
      • 팀프로젝트 (18)
        • SNS(키보드워리어) (9)
        • close_sea (9)
      • 개인프로젝트 (1)
        • Around Flavor (1)
        • CHAM (13)
        • ethFruitShop (5)
      • 독서 (0)
        • 스프링부트와 AWS로 혼자 구현하는 웹 서비스 (0)
  • 블로그 메뉴

    • 홈
    • 개발일기
    • CS
    • 실습
    • 코딩테스트
    • 웹
    • Go
    • node.js
    • 팀플
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    30일 챌린지
    도커
    erc20
    node.js
    CSS
    하이퍼레저
    ERC721
    nodejs
    명령어
    node
    html
    js
    go언어
    go
    React
    mysql
    컨테이너
    30일챌린지
    프로그래머스
    블록체인
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.1
TeTedo.
[자료구조] 스택-큐
상단으로

티스토리툴바