✍️
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
  • stack overflow
  • stack underflow
  • 사용 사례
  • Stack으로 Queue만들기
  • 구현

Was this helpful?

  1. data-structure

Stack

PreviousHeapNextweb

Last updated 5 years ago

Was this helpful?

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

stack에 데이터를 추가하는걸 push라고 부르고 데이터를 꺼낼때 pop이라고 부른다

stack에는 제일 위 데이터를 가리키는 top가 있다

stack overflow

스택에 더 이상 데이터가 추가될 공간이 없는데 추가(push)를 할 때 발생

stack underflow

스택에 메모리가 비어있을 때 데이터를 꺼내려고(pop) 했을 때 발생

사용 사례

  • 역순 문자열 만들기

  • 재귀 알고리즘

  • 웹 브라우저 방문기록 (뒤로가기, 앞으로 가기)

  • 실행 취소 (undo)

    -

Stack으로 Queue만들기

    1. mainStack와 subStack를 만든다

    1. 데이터가 enqueue가 되면 mainStack의 데이터를 pop해서 subStack에 push한다

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

    1. 다시 subStack에서 데이터들을 pop해서 mainStack에 push한다

class Queue {
  constructor() {
    this.mainStack = []
    this.subStack = []
  }

  push(data) {
    while (this.mainStack.length) {
      this.subStack.push(this.mainStack.shift())
    }
    this.subStack.push(data)
    while (this.subStack.length) {
      this.mainStack.push(this.subStack.shift())
    }
  }
  pop() {
    return this.mainStack.shift()
  }
  peak() {
    return this.mainStack[this.mainStack.length - 1]
  }
  isEmpty() {
    return !this.mainStack.length
  }
}

구현

function Node(data) {
  this.data = data
  this.next = null
}
class Stack {
  constructor() {
    this.top = null
  }
  push(data) {
    const n = new Node(data)
    n.next = this.top
    this.top = n
  }

  pop() {
    if (!this.top) {
      throw new Error('stack underflow')
    }
    const item = this.top
    this.top = item.next
    return item.data
  }

  peek() {
    if (!this.top) {
      throw new Error('stack underflow')
    }
    return this.top.data
  }

  isEmpty() {
    return !this.top
  }
}

const stack = new Stack()