Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
Tags
- 자료구조
- OOP
- 인접리스트
- 프로그래머스
- Deep Dive
- nestjs
- bean
- 코딩테스트
- 인접행렬
- TIL
- typescript
- LifeCycle
- Linux
- puppeteer
- 알고리즘
- css
- java
- node.js
- REST API
- Interceptor
- html
- JWT
- Kubernetes
- 탐욕법
- MySQL
- winston
- GraphQL
- dfs
- javascript
- Spring
Archives
- Today
- Total
처음부터 차근차근
[Javascript] 이중 연결 리스트 구현 본문
728x90
이번에는 이중 연결 리스트를 구현했다.
Doublylinkedlist 구현
// 이중 연결 리스트 Class Node
class Node {
constructor(data) {
this.data = data;
this.next = null;
// 이중 연결 리스트이기 때문에 이전 값도 추가되어야 한다.
this.prev = null;
}
}
class DoublyLinkedList {
constructor() {
// 처음 만들었을 경우, head는 null을 넣어야 한다.
this.head = null;
// 마지막도 null을 해준다.
this.tail = null;
// 연결 리스트의 크기를 보기 위해 삽입
this.length = 0;
}
pushFirst(data) {
let newNode = new Node(data);
let current = this.head;
// 노드가 0인 경우 노드만 추가하고, head와 tail로 지정
if (!current) {
this.length++;
this.head = newNode;
this.tail = newNode;
return;
}
// 현재 head의 prev를 newNode로 지정
current.prev = newNode;
// 새로운 노드의 next를 current로 변경
newNode.next = current;
// 헤드 변경
this.head = newNode;
// length 추가
this.length++;
}
pushLast(data) {
let newNode = new Node(data);
// current는 tail로 지정 (뒤에서 부터 삽입하기 위해)
let current = this.tail;
// 노드가 0인 경우 노드만 추가하고, head와 tail로 지정
if (!current) {
this.length++;
this.head = newNode;
this.tail = newNode;
return;
}
// 현재 head의 next를 newNode로 지정
current.next = newNode;
// newNode의 prev를 current로 지정
newNode.prev = current;
// tail로 newNode 지정
this.tail = newNode;
this.length++;
}
popFirst() {
if (this.length === 0) return console.log("Node가 없습니다.");
this.head = this.head.next;
this.length--;
}
popLast() {
if (this.length === 0) return console.log("Node가 없습니다.");
this.tail = this.tail.prev;
this.length--;
}
pushAt(data, index) {
if (index > this.length)
return console.log("총 Node보다 큰 값을 입력했습니다.");
let idx = 0;
let current = this.head;
let previous;
let newNode = new Node(data);
while (idx < index) {
// 이전 것을 먼저 현재 것으로 지정한다.
previous = current;
// 다음 것을 지정한다.
current = current.next;
idx++;
}
newNode.next = current;
newNode.prev = previous;
previous.next = newNode;
current.prev = newNode;
this.length++;
}
popAt(data, index) {
if (index > this.length)
return console.log("총 Node보다 큰 값을 입력했습니다.");
let idx = 0;
let current = this.head;
let previous;
while (idx < index) {
previous = current;
current = current.next;
idx++;
}
previous.next = current.next;
current.next.prev = previous;
this.length--;
}
printNode() {
return this.length;
}
find(index) {
if (this.size === 0) return null;
if (index > this.size) return null;
let current = this.head;
for (let i = 0; i < index; i++) {
current = current.next;
}
return current.data;
}
findindex(data) {
if (this.size === 0) return -1;
let current = this.head;
let index = 0;
while (current !== null) {
if (current.data === data) {
return index;
}
current = current.next;
index++;
}
return -1;
}
}
아직까지 예외 처리나 그런 부분이 미숙하지만, 일단 한번 구현해보면서, LinkedList의 개념을 다시 한번 복습하는 기회가 되었다.
'CS > 자료구조' 카테고리의 다른 글
[Javascript] Hash Table 구현 (0) | 2023.11.07 |
---|---|
[자료구조] Hash (0) | 2023.11.07 |
[자료구조] Queue, Stack (1) | 2023.11.03 |
[Javascript] 단일 연결 리스트 구현 (0) | 2023.11.03 |
[자료구조] Linkedlist, Array (0) | 2023.11.02 |