이중 연결 리스트

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

https://visualgo.net/en/list

 

양방향으로 연결이 되어있는 구조 (prev , next)

기본적인 구조는 단일 연결 리스트와 같기에 설명은 생락하고 바로 메소드를 작성하겠다

prev와 next를 둘다 연결해야하기에 코드가 좀 더 복잡해지지만 flexible하게 사용가능해진다

class Node{
    constructor(val){
        this.val = val;
        this.next = null;
        this.prev = null;
    }
  }
  
  
  class DoublyLinkedList {
    constructor(){
        this.head = null;
        this.tail = null;
        this.length = 0;
    }
    push(val){
      var newNode=new Node(val)
      if(this.length===0){
        this.head=newNode
        this.tail=newNode
      }
      else{
        this.tail.next=newNode
        newNode.prev=this.tail
        this.tail=newNode
      }
      this.length++
      return this
    }
    pop(){
      if(!this.head) return undefined
      var poppedNode=this.tail;
      if(this.length===1){
        this.head=null
        this.tail=null
      }else{
        this.tail=poppedNode.prev
        this.tail.next=null
        poppedNode.prev=null
      }
      this.length--
      return poppedNode
    }
    shift(){
      if(this.length===0)return undefined
      var oldHead=this.head
      if(this.length===1){
        this.head=null
        this.tail=null
      }
      else{
      this.head=oldHead.next
      this.head.prev=null
      oldHead.next=null
      }
      this.length--
      return oldHead
    }
    unshift(val){
      var newNode=new Node(val)
      if(this.length===0) {
        this.head=newNode
        this.tail=newNode
      }
      else{
        this.head.prev=newNode
        newNode.next=this.head
        this.head=newNode
      }
      this.length++
      return this
    }
    get(index){
      if(index<0||index>=this.length) return null
      var count=0;
      var current=this.head
      if(index<=this.length/2){
        while(count!=index){
          current=current.next
          count++
        }
      }else{
        var count=this.length-1
        var current=this.tail
        while(count!==index){
          current=current.prev
          count--
        }
      }
      return current
    }
    set(index,val){
      var foundNode=this.get(index)
      if(foundNode!=null){
        foundNode.val=val
        return true
      }
      return false
    }
    insert(index,val){
      if(index<0 || index > this.length) return false
      if(index===0) return !!this.unshift(val)
      if(index===this.length) return !!this.push(val)
      var newNode=new Node(val)
      var beforeNode=this.get(index-1)
      var afterNode=beforeNode.next
      beforeNode.next=newNode
      newNode.prev=beforeNode
      newNode.next=afterNode
      afterNode.prev=newNode
      this.length++
      return true
    }
    remove(index){
      if(index<0 || index>=this.length) return undefined
      if(index===0) return this.shift()
      if(index===this.length-1) return this.pop()
      var removeNode=this.get(index)
      removeNode.prev.next=removeNode.next
      removeNode.next.prev=removeNode.prev
      removeNode.next=null
      removeNode.prev=null
      this.length--
      return removeNode
    }
  }

var list = new DoublyLinkedList()
list.push("Harry")
list.push("Ron")
list.push("Hermione")

 

728x90
반응형
저작자표시 (새창열림)

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

트리  (1) 2022.08.22
트리 순회  (1) 2022.08.22
단일 연결 리스트  (2) 2022.08.08
문제 해결 패턴  (1) 2022.06.22
Big O  (0) 2022.06.02
'알고리즘(Algorithm)' 카테고리의 다른 글
  • 트리
  • 트리 순회
  • 단일 연결 리스트
  • 문제 해결 패턴
Hun-bot
Hun-bot
IT를 중심으로 다양한 것
  • Hun-bot
    로봇이 만드는 눈사람
    Hun-bot
  • 전체
    오늘
    어제
    • All Article (132)
      • 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) (44)
      • JavaScript (40)
      • Linux (7)
      • JSP (1)
  • 블로그 메뉴

    • 링크

    • 공지사항

    • 인기 글

    • 태그

      JS #정규표현식
      JS #프로그래머스 #숫자의표현 #알고리즘
      LeetCode #JS #Javascript #Algorithm
      프로그래머스
      자바스크립트
      async await #js #문법 #자바스크립트 #비동기
      갭체크
      JS #JavaScript #프로그래머스 #카카오
      알고리즘 #Algorithm
      JS #클래스
      Programmers
      codetree
      코드트리
      프로그래머스 #자바스크립트 #JS
      BaekJoon
      코드트리후기
      알고리즘
      티스토리챌린지
      c++
      자바스크립트 #연습문제
      리눅스
      JSP #Vscode #톰켓 #Tomcat #Java #Web #jdk
      코딩테스트
      Python #알고리즘
      Algorithm
      Javascript
      SQL
      JS #JavaScript #프로그래머스 #알고리즘
      오블완
      Vue #Vue.js #정리
    • 최근 댓글

    • hELLO· Designed By정상우.v4.10.3
    Hun-bot
    이중 연결 리스트
    상단으로

    티스토리툴바

    티스토리툴바