JavaScript 데이터 구조: 단일 연결 목록: 푸시
13452 단어 tutorialwebdevjavascriptbeginners
소개
Last time , 우리는 단일 연결 목록을 설정했습니다.
오늘 우리는 목록에 무언가를 푸시하는 방법을 배웁니다.
Push는 add something to the end를 의미합니다.지난 시간 요약
Node Singly Linked List Node 클래스 Singly Linked List 클래스 현재 코드
// name of the class
class Node {
// the constructor runs when using the class with `new` (see later)
constructor(value) {
// set this nodes value property to the instantiation value
this.value = value;
// set this nodes next property to `null`
this.next = null;
}
}
// name of the class
class SinglyLinkedList {
// the constructor runs when using the class with `new`
constructor() {
// set this lists length property to `0`
this.length = 0;
// set this lists head property to `null`
this.head = null;
// set this lists tail property to `null`
this.tail = null;
}
}
생각
먼저 제약 조건과 가능성에 대해 생각해야 합니다.
Singly Linked List에 이미 하나 이상의 다른 노드가 있는 경우:
next 속성이 새 노드를 가리키도록 합니다(따라서 새 노드가 현재 꼬리 뒤에 옵니다). tail을 새 노드현재 단일 연결 목록에 다른 노드가 없는 경우(따라서 현재 비어 있음):
head을 새 노드tail을 새 노드차이점들?
예시:
따라서
A 는 항상 헤드를 유지하며 0 노드가 있는 경우에만 A 를 새 head 로 설정해야 합니다.다른 모든 상황에서는 현재
tail가 새 노드를 가리키도록 하고 새 노드를 새 노드tail로 만들어야 합니다.구현(긴 버전, DRY 없음)
Thoughts class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
class SinglyLinkedList {
constructor() {
this.length = 0;
this.head = null;
this.tail = null;
}
push(value) {
/*
* 1. If there is already at least one other node in the Singly Linked List
*/
if (this.length > 0) {
// create a new node with an input value
const newNode = new Node(value);
// point the current tails `next` property to the new node
this.tail.next = newNode;
// set the Singly Linked List's `tail` to the new node
this.tail = newNode;
// increase the Singly Linked List's length by 1
this.length += 1;
// return the new node
return newNode;
} else {
/*
* 2. If there is currently NO other node in the Singly Linked List (so it is currently empty):
*/
// create a new node with an input value
const newNode = new Node(value);
// set the Singly Linked List's `head` to the new node
this.head = newNode;
// set the Singly Linked List's `tail` to the new node
this.tail = newNode;
// increase the Singly Linked List's length by 1
this.length += 1;
// return the new node (so that we knew what we added)
return newNode;
}
}
}
구현(짧은 버전, DRY)
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
class SinglyLinkedList {
constructor() {
this.length = 0;
this.head = null;
this.tail = null;
}
push(value) {
const newNode = new Node(value);
if (this.length > 0) {
this.tail.next = newNode;
} else {
this.head = newNode;
}
this.tail = newNode;
this.length += 1;
return newNode;
}
}
결과
Singly Linked List
push 방법과 그 결과를 살펴보자.const newSLL = new SinglyLinkedList();
console.log(newSLL);
/*
* SinglyLinkedList {
* length: 0,
* head: null,
* tail: null
* }
*/
newSLL.push("A");
console.log(newSLL);
/*
* SinglyLinkedList {
* length: 1,
* head: Node { value: 'A', next: null },
* tail: Node { value: 'A', next: null }
* }
*/
newSLL.push("B");
console.log(newSLL);
/*
* SinglyLinkedList {
* length: 2,
* head: Node { value: 'A', next: Node { value: 'B', next: null } },
* tail: Node { value: 'B', next: null }
* }
*/
다음 부분
Singly Linked List의 끝에서 노드를 제거하는 방법을 구현합니다. 알림을 원하시면 subscribe :)
Reference
이 문제에 관하여(JavaScript 데이터 구조: 단일 연결 목록: 푸시), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/miku86/javascript-data-structures-singly-linked-list-push-3el0텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)