ㅋㅋㅋ 감사하네요ㅋㅋㅋㅋ
고치진 않았습니다~~~
ㅋㅋㅋㅋ
singly
class Node { constructor(value) { this.value = value; this.next = null; } } class SinglyLinkedList { constructor() { this.head = null; this.tail = null; } //value요소 탐색 // - 에러경우라고 생각했네요 : 원하는 요소를 찾지 못했을 때 // - find(value) { let curNode = this.head; while (curNode != null) { if (curNode.value === value) break; curNode = curNode.next; } return curNode; } //맨 뒤에 요소 추가 append(newValue) { let newNode = new Node(newValue); if (this.head === null) { this.head = newNode; this.tail = newNode; } else { this.tail.next = newNode; this.tail = newNode; } } /* node 다음 newValue 추가 * 1. 구현 * - newNode.next -> node.next; * - node.next -> newNode; * 2. 에러 처리 * - node === null? (기준이 되는 요소가 없을 시) -> 맨 마지막 요소로 추가 - 그렇지 않으면 삽입합니다. */ insert(node, newValue) { let newNode = new Node(newValue); if (node === null) this.append(newValue); else { newNode.next = node.next; node.next = newNode; if (newNode.next === null) this.tail = newNode; // tail update } } /* value 추가 * 1. 구현 * - prevNode.next -> prevNode.next.next; * 2. 에러 처리 * - 리스트가 비어있는 경우 * - 삭제할 요소가 헤더 * - 요소를 찾지 못했을 경우 (prevNode === tail) */ remove(value) { let prevNode = this.head; if (prevNode === null) return false; if (prevNode.value === value) { this.head = this.head.next; return true; } while (prevNode.next !== null) { if (prevNode.next.value === value) break; prevNode = prevNode.next; } if (prevNode.next !== null) { prevNode.next = prevNode.next.next; if (prevNode.next === null) this.tail = prevNode; //tail update return true; } return false; } //요소 길이 반환 size() { let curNode = this.head; let size = 0; while (curNode !== null) { size++; curNode = curNode.next; } return size; } display() { let curNode = this.head; let displayString = "["; while (curNode !== null) { displayString += `${curNode.value}, `; curNode = curNode.next; } displayString = displayString .substr(0, displayString.length - 2); displayString += "]"; return displayString; } } const linkedList = new SinglyLinkedList(); //append linkedList.append(1); linkedList.append(2); linkedList.append(3); linkedList.append(4); linkedList.append(5); console.log(linkedList.display() + ` size : ${linkedList.size()}`); //find console.log(linkedList.find(1)); console.log(linkedList.find(2)); console.log(linkedList.find(5)); console.log(linkedList.find(10) ? linkedList.find(10) : "Not Found"); //insert linkedList.insert(linkedList.find(2), 10); linkedList.insert(linkedList.find(100), 11); console.log(linkedList.display() + ` size : ${linkedList.size()}`); //remove console.log(linkedList.remove(1)); console.log(linkedList.remove(2)); console.log(linkedList.remove(11)); //last element console.log(linkedList.remove(100)); console.log(linkedList.display() + ` size : ${linkedList.size()}`); //insert(check tail update) linkedList.append(11); console.log(linkedList.display() + ` size : ${linkedList.size()}`); linkedList.insert(linkedList.find(11), 12); linkedList.append(13); console.log(linkedList.display() + ` size : ${linkedList.size()}`);
doubly
class Node { constructor(value) { this.value = value; this.next = null; this.prev = null; } } class DoublyLinkedList { constructor() { this.head = null; this.tail = null; } //value요소 탐색 // - 원하는 요소를 찾지 못했을 때 find(value) { let curNode = this.head; while (curNode != null) { if (curNode.value === value) break; curNode = curNode.next; } return curNode; } //맨 뒤에 요소 추가 append(newValue) { let newNode = new Node(newValue); if (this.head === null) { this.head = newNode; this.tail = newNode; } else { this.tail.next = newNode; newNode.prev = this.tail; this.tail = newNode; } } /* node 다음 newValue 추가 * 1. 구현 * - newNode.next -> node.next; * - node.next -> newNode; * - node.next.prev -> newNode; * - newNode.prev -> node; * 2. 에러 처리 * - node === null? (기준이 되는 요소가 없을 시) * - node === this.tail */ insert(node, newValue) { let newNode = new Node(newValue); if (node === null) this.append(newValue); else { newNode.next = node.next; node.next = newNode; if (node.next !== null) node.next.prev = newNode; newNode.prev = node; if (newNode.next === null) this.tail = newNode; //tail update } } /* value 추가 * 1. 구현 * - prevNode.next -> prevNode.next.next; * 2. 에러 처리 * - 리스트가 비어있는 경우 * - 삭제할 요소가 헤더 * - 요소를 찾지 못했을 경우 (prevNode === tail) */ remove(value) { let prevNode = this.head; if (prevNode === null) return false; if (prevNode.value === value) { this.head = this.head.next; return true; } while (prevNode.next !== null) { if (prevNode.next.value === value) break; prevNode = prevNode.next; } if (prevNode.next !== null) { prevNode.next = prevNode.next.next; if (prevNode.next !== null) prevNode.next.prev = prevNode; if (prevNode.next === null) this.tail = prevNode; // tail Updeate return true; } return false; } //요소 길이 반환 size() { let curNode = this.head; let size = 0; while (curNode !== null) { size++; curNode = curNode.next; } return size; } display() { let curNode = this.head; let displayString = "["; while (curNode !== null) { displayString += `${curNode.value}, `; curNode = curNode.next; } displayString = displayString .substr(0, displayString.length - 2); displayString += "]"; return displayString; } } const linkedList = new DoublyLinkedList(); //append linkedList.append(1); linkedList.append(2); linkedList.append(3); linkedList.append(4); linkedList.append(5); console.log(linkedList.display() + ` size : ${linkedList.size()}`); //find console.log(linkedList.find(1)); console.log(linkedList.find(2)); console.log(linkedList.find(5)); console.log(linkedList.find(10) ? linkedList.find(10) : "Not Found"); // //insert linkedList.insert(linkedList.find(2), 10); linkedList.insert(linkedList.find(100), 11); console.log(linkedList.display() + ` size : ${linkedList.size()}`); // //remove console.log(linkedList.remove(1)); console.log(linkedList.remove(2)); console.log(linkedList.remove(11)); console.log(linkedList.remove(100)); console.log(linkedList.display() + ` size : ${linkedList.size()}`); // //insert(check tail update) linkedList.append(11); console.log(linkedList.display() + ` size : ${linkedList.size()}`); linkedList.insert(linkedList.find(11), 12); linkedList.append(13); console.log(linkedList.display() + ` size : ${linkedList.size()}`);
circular
class Node { constructor(value) { this.value = value; this.next = null; } } class CircularLinkedList { constructor() { this.head = null; this.tail = null; this.size = 0; } //value요소 탐색 // - 원하는 요소를 찾지 못했을 때 find(value) { let curNode = this.head; let count = 1; while (count <= this.size) { if (curNode.value === value) break; count++; curNode = curNode.next; } return count > this.size ? null : curNode; } //맨 뒤에 요소 추가 append(newValue) { let newNode = new Node(newValue); if (this.head === null) { this.head = newNode; } else { this.tail.next = newNode; } newNode.next = this.head; this.size++; //size update this.tail = newNode; //tail update } /* node 다음 newValue 추가 * 1. 구현 * - newNode.next -> node.next; * - node.next -> newNode; * 2. 에러 처리 * - node === null? (기준이 되는 요소가 없을 시) * - node === this.tail */ insert(node, newValue) { let newNode = new Node(newValue); if (node === null) this.append(newValue); else { newNode.next = node.next; node.next = newNode; this.size++; if (newNode.next === this.head) this.tail = newNode; } } /* value 추가 * 1. 구현 * - prevNode.next -> prevNode.next.next; * 2. 에러 처리 * - 리스트가 비어있는 경우 * - 삭제할 요소가 헤더 * - 요소를 찾지 못했을 경우 (prevNode === tail) */ remove(value) { let prevNode = this.head; let count = null; if (prevNode === null) return false; if (prevNode.value === value) { this.head = this.head.next; this.tail.next = this.head; this.size--; return true; } count = 1; while (count < this.size) { if (prevNode.next.value === value) break; count++; prevNode = prevNode.next; } if (count !== this.size) { prevNode.next = prevNode.next.next; if (prevNode.next === this.head) this.tail = prevNode; // tail Updeate this.size--; //size update return true; } return false; } //요소 길이 반환 getSize() { return this.size; } display() { let curNode = this.head; let displayString = "["; let count = 1; while (count <= this.size) { displayString += `${curNode.value}, `; count++; curNode = curNode.next; } displayString = displayString .substr(0, displayString.length - 2); displayString += "]"; return displayString; } circularDisplay(count) { let curNode = this.head; let displayString = "["; let countNum = count; while (countNum--) { displayString += `${curNode.value}, `; curNode = curNode.next; } displayString = displayString .substr(0, displayString.length - 2); displayString += "]"; return displayString; } } const linkedList = new CircularLinkedList(); //append linkedList.append(1); linkedList.append(2); linkedList.append(3); linkedList.append(4); linkedList.append(5); console.log(linkedList.display() + ` size : ${linkedList.getSize()}`); //find console.log(linkedList.find(1)); console.log(linkedList.find(2)); console.log(linkedList.find(5)); console.log(linkedList.find(10) ? linkedList.find(10) : "Not Found"); //insert linkedList.insert(linkedList.find(2), 10); linkedList.insert(linkedList.find(100), 11); console.log(linkedList.display() + ` size : ${linkedList.getSize()}`); //remove console.log(linkedList.remove(1)); console.log(linkedList.remove(2)); console.log(linkedList.remove(11)); console.log(linkedList.remove(100)); console.log(linkedList.display() + ` size : ${linkedList.getSize()}`); //insert(check tail update) linkedList.append(11); console.log(linkedList.display() + ` size : ${linkedList.getSize()}`); linkedList.insert(linkedList.find(11), 12); linkedList.append(13); console.log(linkedList.display() + ` size : ${linkedList.getSize()}`); //circularDisplay console.log(linkedList.circularDisplay(15));