머리와 꼬리를 null로 초기화하는 생성자로 간단한 클래스를 정의하는 것으로 시작하겠습니다. 또한 연결 목록의 각 노드를 나타내는 DoublyLinkedList 클래스의 프로토타입에 다른 구조를 정의합니다.
예시
class LinkedList {
constructor() {
this.head = null;
this.tail = null;
this.length = 0;
}
}
LinkedList.prototype.Node = class {
constructor(data) {
this.data = data;
this.next = null;
this.prev = null;
}
}; 목록이 어떻게 생겼는지 확인하는 데 도움이 되는 표시 기능도 만들어 보겠습니다. 이 기능은 다음과 같이 작동합니다.
- 머리부터 시작합니다.
- currElem이 null이 되지 않을 때까지, 즉 끝에 도달하지 않을 때까지 currElem =currElem.next를 사용하여 목록을 반복합니다.
- 각 반복에 대한 데이터를 인쇄합니다.
다음은 동일한 그림입니다 -

이제 이를 구현하는 방법을 살펴보겠습니다.
예시
display() {
let currNode = this.head;
while (currNode != null) {
console.log(currNode.data + " -> ");
currNode = currNode.next;
}
}