JS 알고리즘 15일차 - 이중 연결 리스트(insert, remove)


JS 알고리즘 15일차 - 이중 연결 리스트(insert, remove)

Insert 소개 인덱스와 값을 받아 해당 인덱스에 노드를 추가한다. 의사코드 인덱스가 유효한지 확인한다. 인덱스가 0이면 unshift, 인덱스가 this.length면 push를 사용한다. 그렇지 않다면 get을 사용해서 우리가 삽입하려고 하는 값을 바로 전에 값 next로 지정한다. 그리고 next와 prev를 사용해 노드를 연결한다. 마지막으로 길이를 1 증가시키고 true를 출력한다. 코드 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; } get(index) { let current = null; if (index < 0 || index >= this.length) return false; if (i...


#insert #JavaScript #remove #알고리즘 #이중연결리스트 #자료구조

원문링크 : JS 알고리즘 15일차 - 이중 연결 리스트(insert, remove)