연결 리스트는 노드의 연결로 이루어진 자료구조로 각각의 노드는 데이터와 다음 노드의 주소를 가지고 있어 서로를 연결한다.
노드는 크기가 동적인 자료구조로, 자료구조를 구성하는 요소이다.
undefined
를 반환한다.위 그림과 같이 새로운 노드를 추가를 하게되면 b는 c를 바라보게 되고 c가 tail이 되면서 c의 next는 null이 된다.
addToTail(value) {
const node = new Node(value);
if (this.head === null) {
this.head = node;
this.tail = node;
} else {
this.tail.next = node;
this.tail = node;
}
this._size++;
return this;
}
위 그림과 같이 tail에 값을 삭제하면 그 이전의 node가 tail이 되고 next는 null이 된다.
remove(value) {
if (this.head === null) {
return undefined;
} else if (value === this.head.value) {
this.head = this.head.next;
} else {
this.head.next = this.head.next.next;
}
this._size--;
}
getNodeAt(index) {
let count = 0;
let node = this.head;
while (count !== index) {
count++;
node = node.next;
if (index > this._size) {
return undefined;
}
}
return node;
}
contains(value) {
let count = 0;
let node = this.head;
while (node.value !== value) {
count++;
node = node.next;
if (this._size <= count) {
return false;
}
}
return true;
}
indexOf(value) {
let count = 0;
let node = this.head;
while (node.value !== value) {
count++;
node = node.next;
if (this._size <= count) {
return -1;
}
}
return count;
}