
📅 2025-10-29
➡️ 자료구조 연결리스트에 대해 새롭게 알게 된 것 또는 헷갈리는 부분 정리

value)과 다음 노드에 대한 참조(next)를 가지고 있음head)라 부름null을 가리킴const list = {
head: {
value: 6
next: {
value: 10
next: {
value: 12
next: {
value: 3
next: null
}
}
}
}
}
};
장점
단점
class Node {
constructor(data, next = null, length) {
this.data = data;
this.next = next;
}
}
head가 null → 새 노드를 head로 설정next에 새 노드를 연결class Node {
constructor(data, next = null) {
this.data = data;
this.next = next;
}
}
class LinkedList {
constructor() {
this.head = null;
this.length = 0;
}
add(value){
const newNode = new Node(value);
if (!this.head) {
// 리스트가 비어 있으면, head가 새 노드
this.head = newNode;
} else {
// 마지막 노드까지 이동
let current = this.head;
while (current.next) {
current = current.next;
}
// 마지막 노드의 next에 새 노드 연결
current.next = newNode;
}
this.length++;
console.log(JSON.stringify(list, null, this.length));
}
remove() {}
search() {}
}
search(index) {
let current = this.head;
let count = 0;
// index 값까지 이동
while (count < index) {
current = current.next;
count++
}
// current가 존재하면 data 반환, 없으면 null
return current ? current.data : null;
}
remove(value) {
if (!this.head) return;
// head가 삭제 대상이면
if (this.head.data === value) {
this.head = this.head.next;
this.length--;
return;
}
let current = this.head;
while (current.next) {
if (current.next.data === value) {
current.next = current.next.next;
this.length--;
return;
}
current = current.next;
}
}