스택+큐

2022. 8. 23. 21:16·알고리즘(Algorithm)
728x90
반응형

스택

데이터의 모음 -> 압축적인 데이터 구조 LIFO(Last In First Out) 후입 선출

- Undo/Redo 기능

-인터넷 방문 기록

Big O 

*Insertion - O(1)

*Removal - O(1)

Searching - O(N)

Access - O(N)

 

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

class Stack{
  constructor(){
    this.first=null;
    this.last=null;
    this.size=0
  }
  push(val){
    var newNode=new Node(val)
    if(!this.first){
      this.first=newNode
      this.last=newNode
    }else{
      var temp=this.first
      this.first=newNode
      this.first.next=temp
    }
    return ++this.size
  }
  pop(){
    if(this.size===0) return null
    var temp=this.first
    if(this.size===1) {
      this.last=null
    }
    this.first=this.first.next
    this.size--
    return temp.value
  }
}

큐

FIFO(First In First Out) 선입 선출 (추가 - enqueue 제거 - dequeue) 

- 접속 대기

- 줄

- 업로드

- 프린트 대기열

- 업무 처리할때 유리

Big O 

*Insertion - O(1)

*Removal - O(1)

Searching - O(N)

Access - O(N)

 

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

class Queue {
    constructor(){
        this.first = null;
        this.last = null;
        this.size = 0;
    }
    enqueue(val){
        var newNode = new Node(val);
        if(!this.first){
            this.first = newNode;
            this.last = newNode;
        } else {
            this.last.next = newNode;
            this.last = newNode;
        }
        return ++this.size;
    }

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

        var temp = this.first;
        if(this.first === this.last) {
            this.last = null;
        }
        this.first = this.first.next;
        this.size--;
        return temp.value;
    }
}
728x90
반응형
저작자표시 (새창열림)

'알고리즘(Algorithm)' 카테고리의 다른 글

해시 테이블(HASH TABLES)  (0) 2022.08.30
힙 + 우선 순위 큐  (0) 2022.08.26
트리  (0) 2022.08.22
트리 순회  (1) 2022.08.22
이중 연결 리스트  (0) 2022.08.16
'알고리즘(Algorithm)' 카테고리의 다른 글
  • 해시 테이블(HASH TABLES)
  • 힙 + 우선 순위 큐
  • 트리
  • 트리 순회
Hun-bot
Hun-bot
IT를 중심으로 다양한 것
  • Hun-bot
    로봇이 만드는 눈사람
    Hun-bot
  • 전체
    오늘
    어제
    • All Article (128)
      • Programmers (6)
        • TIP (1)
        • SQL (2)
        • LV1 (1)
        • LV2 (2)
        • LV3 (0)
      • Baekjoon (31)
        • Bronze (10)
        • Silver (19)
        • Gold (2)
        • Platinum (0)
        • Diamond (0)
      • Leetcode (0)
        • Easy (0)
        • Medium (0)
        • Hard (0)
        • SQL (0)
      • 알고리즘(Algorithm) (42)
      • JavaScript (40)
      • Linux (7)
      • JSP (1)
  • 블로그 메뉴

    • 링크

    • 공지사항

    • 인기 글

    • 태그

      자바스크립트 #연습문제
      자바스크립트
      JS #JavaScript #프로그래머스 #알고리즘
      JS #클래스
      JavaScript #Set #Collection
      알고리즘
      JS #정규표현식
      오블완
      알고리즘 #Algorithm
      프로그래머스
      JS #javascript #객체 #Object
      티스토리챌린지
      JS #JavaScript #프로그래머스 #카카오
      JS #프로그래머스 #숫자의표현 #알고리즘
      Vue #Vue.js #정리
      Programmers
      리눅스 #입문
      async await #js #문법 #자바스크립트 #비동기
      프로그래머스 #자바스크립트 #JS
      Algorithm
      LeetCode #JS #Javascript #Algorithm
      JSP #Vscode #톰켓 #Tomcat #Java #Web #jdk
      c++
      리눅스
      파이썬 #입력 #python #input
      Javascript
      BaekJoon
      고득점 Kit
      Python #알고리즘
      SQL
    • 최근 댓글

    • hELLO· Designed By정상우.v4.10.3
    Hun-bot
    스택+큐
    상단으로

    티스토리툴바

    티스토리툴바