✍️
TIL
  • TIL
  • react
    • react 16.3 이후 라이프 싸이클
    • 함수형 컴포넌트 vs 클래스 컴포넌트
    • React의 장점
    • 기본문법
    • Flow Diagram
    • redux-saga
    • nextjs
    • mobx
    • 리액트는 어떻게 동작할까?
  • data-structure
    • 이진 검색 트리(binary search tree)
    • HashTable
    • Tree
    • 트라이(Trie)
    • 선형 구조 vs 비선형 구조
    • 연결 리스트(linked-list)
    • Queue
    • Graph
    • Heap
    • Stack
  • web
    • 웹 브라우저의 동작 원리
    • Basic
    • Webpack이란?
    • rendering
    • npm
    • babel
  • graphQL
    • Query
    • Mutation
    • Introduction
  • algorithm
    • big-o
    • 버블 정렬(bubble sort)
    • 힙 정렬(heap sort)
    • 선택 정렬(selection sort)
    • 퀵 정렬(quick sort)
    • 백트래킹 알고리즘
    • 삽입 정렬(insertion sort)
    • 계수 정렬(counting sort)
    • 다엑스트라 알고리즘
    • 이진 탐색
    • 합병 정렬(merge-sort)
    • 동적 계획법(Dynamic programming)
  • web-security
    • XSS(Cross Site Scripting)
    • CSRF(Cross-site request forgery)
    • Tabnabbing
  • javaScript
    • dom
    • 자바스크립트 성능 최적화
    • Event Loop
    • Snippets
    • javaScript
  • programming-paradigm
    • Object Oriented Programming
    • 함수형 프로그래밍
    • 구조적 프로그래밍
  • computer-science
    • Process vs Thread
    • 비트 연산자
    • 그레이 코드
  • vue
    • Vue
  • design-pattern
    • MVP pattern
    • Flux
    • 아토믹 디자인
    • MVVM pattern
    • MVC pattern
  • css
    • css
    • Grid
    • css-methodologies
    • FlexBox
  • html
    • html
  • regExp
    • regExp
  • git
    • Git-flow
Powered by GitBook
On this page
  • Queue로 Stack만들기
  • 구현

Was this helpful?

  1. data-structure

Queue

Previous연결 리스트(linked-list)NextGraph

Last updated 5 years ago

Was this helpful?

가장 먼저 추가된 항목이 가장 먼저 제거되는 FIFO(First In First Out) 형식의 자료 구조

queue에 데이터를 추가하는걸 enqueue라고 부르고 데이터를 꺼낼때 dequeue이라고 부른다

queue에는 제일 위 데이터를 반환하는 peek과 queue가 비어있는지 불리언으로 반환하는 isEmpty 메소드가 있다

Queue로 Stack만들기

    1. mainQueue와 subQueue를 만든다

    1. 데이터가 push 되면 mainQueue의 데이터를 dequeue해서 subQueue에 enqueue한다

    1. mainQueue에 push할 데이터를 enqueue한다

    1. 다시 subQueue에서 dequeue한다음 mainQueue에 enqueue한다

class Stack {
  constructor() {
    this.mainQueue = []
    this.subQueue = []
  }

  push(data) {
    while (this.mainQueue.length) {
      this.subQueue.push(this.mainQueue.shift())
    }
    this.mainQueue.push(data)
    while (this.subQueue.length) {
      this.mainQueue.push(this.subQueue.shift())
    }
  }
  pop() {
    return this.mainQueue.shift()
  }
  top() {
    return this.mainQueue[0]
  }
  isEmpty() {
    return !this.mainQueue.length
  }
}

구현

function CreateNode(data) {
  this.data = data
  this.next = null
}
class Queue {
  constructor() {
    this.head = null
    this.rear = null
  }

  enqueue(data) {
    const node = new CreateNode(data)
    // queue에 아무 data가 없을때
    if (!this.head) {
      this.head = node
      // queue에 data가 있을때
    } else {
      this.rear.next = node
    }
    this.rear = node
  }

  dequeue() {
    if (!this.head) {
      throw new Error('queue에 data가 없습니다')
    }
    const item = this.head.data
    this.head = this.head.next
    // queue에 data가 없을때
    if (!this.head) {
      this.rear = null
    }
    return item
  }

  peek() {
    return this.head.data
  }

  isEmpty() {
    return !this.head
  }
}

const queue = new Queue()