From 6606795a38865b6e4c9744cba618df55d23c70e5 Mon Sep 17 00:00:00 2001 From: Minhye Kang Date: Fri, 2 Dec 2022 14:11:40 +0900 Subject: [PATCH 1/6] =?UTF-8?q?feat:=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=20=EC=99=84=EB=A3=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\353\257\274\355\230\234/index.js" | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 "JavaScript/07\354\243\274\354\260\250/assignment/7\354\243\274\354\260\250/\353\257\274\355\230\234/index.js" diff --git "a/JavaScript/07\354\243\274\354\260\250/assignment/7\354\243\274\354\260\250/\353\257\274\355\230\234/index.js" "b/JavaScript/07\354\243\274\354\260\250/assignment/7\354\243\274\354\260\250/\353\257\274\355\230\234/index.js" new file mode 100644 index 0000000..1bf8c62 --- /dev/null +++ "b/JavaScript/07\354\243\274\354\260\250/assignment/7\354\243\274\354\260\250/\353\257\274\355\230\234/index.js" @@ -0,0 +1,91 @@ +// 양방향 연결리스트는 각 노드의 prev와 next로 이동할 수 있어야 한다. +class Node { + constructor(value) { + this.prev = null; + this.value = value; + this.next = null; + } +} + +class DoublyLinkedList { + constructor() { + this.head = null; + this.tail = null; + this.length = 0; + } + /* + push를 통해 양방향 연결리스트 꼬리에 값을 추가할 수 있다. + */ + push(value) { + let newNode = new Node(value); + if (!this.head) { + this.head = newNode; + this.tail = newNode; + } else { + this.tail.next = newNode; + newNode.prev = this.tail; + this.tail = newNode; + } + this.length++; + return this; + } + /* + insert를 통해 특정 인덱스의 노드에 삽입할 수 있어야 한다. + */ + insert(index, value) { + if (index < 0 || index > index.length) return false; + if (index === this.length - 1) return !!this.push(value); + + let newNode = new Node(value); + let previousNode = this.get(index); + + newNode.next = previousNode.next; + newNode.prev = previousNode; + previousNode.next = newNode; + this.length++; + + return true; + } + + /* + get을 통해 원하는 인덱스의 노드를 가져온다. + */ + get(index) { + let counter = 0; + let currentNode = this.head; + while (counter < index) { + currentNode = currentNode.next; + counter++; + } + return currentNode; + } + + /* + remove를 통해 해당 인덱스의 노드를 삭제할 수 있다. + */ + remove(index) { + let currentNode = this.get(index); + let previousNode = currentNode.prev; + let afterNode = currentNode.next; + + previousNode.next = afterNode; + afterNode.prev = previousNode; + this.length--; + + return currentNode; + } + + /* + update를 통해 해당 인덱스의 노드를 수정할 수 있다. + */ + update(index, value) { + if (index < 0 || index > this.length) return false; + + let currentNode = this.get(index); + currentNode.value = value; + + return true; + } +} + +const list = new DoublyLinkedList(); From d3e2c3a822884f28e6530a1905620b76e5b56845 Mon Sep 17 00:00:00 2001 From: Minhye Kang Date: Fri, 2 Dec 2022 16:01:29 +0900 Subject: [PATCH 2/6] =?UTF-8?q?feat:=20pop,=20shift=20=EB=A9=94=EC=84=9C?= =?UTF-8?q?=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\353\257\274\355\230\234/index.js" | 56 ++++++++++++++----- 1 file changed, 43 insertions(+), 13 deletions(-) diff --git "a/JavaScript/07\354\243\274\354\260\250/assignment/7\354\243\274\354\260\250/\353\257\274\355\230\234/index.js" "b/JavaScript/07\354\243\274\354\260\250/assignment/7\354\243\274\354\260\250/\353\257\274\355\230\234/index.js" index 1bf8c62..0110025 100644 --- "a/JavaScript/07\354\243\274\354\260\250/assignment/7\354\243\274\354\260\250/\353\257\274\355\230\234/index.js" +++ "b/JavaScript/07\354\243\274\354\260\250/assignment/7\354\243\274\354\260\250/\353\257\274\355\230\234/index.js" @@ -1,4 +1,6 @@ -// 양방향 연결리스트는 각 노드의 prev와 next로 이동할 수 있어야 한다. +/** + * 양방향 연결리스트는 각 노드의 prev와 next로 이동할 수 있어야 한다. + */ class Node { constructor(value) { this.prev = null; @@ -13,8 +15,9 @@ class DoublyLinkedList { this.tail = null; this.length = 0; } - /* - push를 통해 양방향 연결리스트 꼬리에 값을 추가할 수 있다. + + /** + * push를 통해 양방향 연결리스트 꼬리에 값을 추가할 수 있다. */ push(value) { let newNode = new Node(value); @@ -29,8 +32,8 @@ class DoublyLinkedList { this.length++; return this; } - /* - insert를 통해 특정 인덱스의 노드에 삽입할 수 있어야 한다. + /** + * insert를 통해 특정 인덱스의 노드에 삽입할 수 있어야 한다. */ insert(index, value) { if (index < 0 || index > index.length) return false; @@ -46,10 +49,10 @@ class DoublyLinkedList { return true; } - - /* - get을 통해 원하는 인덱스의 노드를 가져온다. - */ + +/** + * get을 통해 원하는 인덱스의 노드를 가져온다. + */ get(index) { let counter = 0; let currentNode = this.head; @@ -60,8 +63,20 @@ class DoublyLinkedList { return currentNode; } - /* - remove를 통해 해당 인덱스의 노드를 삭제할 수 있다. + /** + * shift를 통해 머리 노드를 가져온다. + */ + shift() { + const shiftedHead = this.head; + this.head = shiftedHead.next; + this.head.prev = null; + this.length--; + + return true; +} + + /** + * remove를 통해 원하는 인덱스의 노드를 삭제한다. */ remove(index) { let currentNode = this.get(index); @@ -75,8 +90,21 @@ class DoublyLinkedList { return currentNode; } - /* - update를 통해 해당 인덱스의 노드를 수정할 수 있다. + /** + * pop 메서드를 통해 꼬리 노드를 꺼낸다. + */ + pop() { + const poppedTail = this.tail; + this.tail = poppedTail.prev; + this.tail.next = null; + + this.length--; + + return poppedTail; + } + + /** + * update를 통해 원하는 인덱스의 노드를 수정할 수 있다. */ update(index, value) { if (index < 0 || index > this.length) return false; @@ -89,3 +117,5 @@ class DoublyLinkedList { } const list = new DoublyLinkedList(); + +module.exports = DoublyLinkedList; From 6b92a2e13d443d0acb226613cb7f2b7732f5f77c Mon Sep 17 00:00:00 2001 From: Minhye Kang Date: Fri, 2 Dec 2022 16:11:53 +0900 Subject: [PATCH 3/6] =?UTF-8?q?refactor:=20shift=20=EB=A9=94=EC=84=9C?= =?UTF-8?q?=EB=93=9C=EC=9D=98=20=EB=B0=98=ED=99=98=EA=B0=92=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\353\257\274\355\230\234/index.js" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/JavaScript/07\354\243\274\354\260\250/assignment/7\354\243\274\354\260\250/\353\257\274\355\230\234/index.js" "b/JavaScript/07\354\243\274\354\260\250/assignment/7\354\243\274\354\260\250/\353\257\274\355\230\234/index.js" index 0110025..40c3f7c 100644 --- "a/JavaScript/07\354\243\274\354\260\250/assignment/7\354\243\274\354\260\250/\353\257\274\355\230\234/index.js" +++ "b/JavaScript/07\354\243\274\354\260\250/assignment/7\354\243\274\354\260\250/\353\257\274\355\230\234/index.js" @@ -72,7 +72,7 @@ class DoublyLinkedList { this.head.prev = null; this.length--; - return true; + return shiftedHead; } /** From d80bd943c28aadbf9eef17585864b1c21b202ce1 Mon Sep 17 00:00:00 2001 From: Minhye Kang Date: Fri, 2 Dec 2022 16:12:38 +0900 Subject: [PATCH 4/6] =?UTF-8?q?test:=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\353\257\274\355\230\234/index.test.js" | 46 +++++++++++++++++++ .../\353\257\274\355\230\234/package.json" | 10 ++++ 2 files changed, 56 insertions(+) create mode 100644 "JavaScript/07\354\243\274\354\260\250/assignment/7\354\243\274\354\260\250/\353\257\274\355\230\234/index.test.js" create mode 100644 "JavaScript/07\354\243\274\354\260\250/assignment/7\354\243\274\354\260\250/\353\257\274\355\230\234/package.json" diff --git "a/JavaScript/07\354\243\274\354\260\250/assignment/7\354\243\274\354\260\250/\353\257\274\355\230\234/index.test.js" "b/JavaScript/07\354\243\274\354\260\250/assignment/7\354\243\274\354\260\250/\353\257\274\355\230\234/index.test.js" new file mode 100644 index 0000000..240a755 --- /dev/null +++ "b/JavaScript/07\354\243\274\354\260\250/assignment/7\354\243\274\354\260\250/\353\257\274\355\230\234/index.test.js" @@ -0,0 +1,46 @@ +const DoublyLinkedList = require('./index'); + +describe("양방향 연결리스트", () => { + let doublyLinkedList; + // 1,2,3이 순서대로 양방향 연결리스트에 들어있습니다. + beforeEach(() => { + doublyLinkedList = new DoublyLinkedList(); + doublyLinkedList.push(1).push(2).push(3); + }); + test("push 메서드로 3개를 추가하면 양방향 연결리스트의 길이는 3이다.", () => { + expect(doublyLinkedList.length).toBe(3); + }); + test("1,2,3을 차례대로 추가했을 때 머리노드 값은 1이고 꼬리노드 값은 3이다.", () => { + expect(doublyLinkedList.head.value).toBe(1) + expect(doublyLinkedList.tail.value).toBe(3) + }) + // 첫번째 머리노드(head)의 인덱스 번호는 0입니다. 두번째 노드의 인덱스 번호는 1입니다. + test("get 메서드로 두번째 노드를 추출한 값은 2이다.", () => { + const extractedNode = doublyLinkedList.get(1); + expect(extractedNode.value).toBe(2); + }) + test("insert 메서드로 두번째 노드 뒤에 2.5를 추가할 수 있다.", () =>{ + doublyLinkedList.insert(1,2.5) + expect(doublyLinkedList.get(1).value).toBe(2) + expect(doublyLinkedList.get(2).value).toBe(2.5) + expect(doublyLinkedList.get(3).value).toBe(3) + expect(doublyLinkedList.length).toBe(4) + }) + test("remove 메서드로 원하는 인덱스의 값을 삭제할 수 있다.", () => { + doublyLinkedList.remove(1); + expect(doublyLinkedList.get(1).value).toBe(3); + expect(doublyLinkedList.length).toBe(2); + }) + test("pop 메서드로 꼬리 노드를 꺼낼 수 있다.", () => { + expect(doublyLinkedList.pop().value).toBe(3); + expect(doublyLinkedList.length).toBe(2); + }) + test("shift 메서드로 머리 노드를 꺼낼 수 있다.", () => { + expect(doublyLinkedList.shift().value).toBe(1); + expect(doublyLinkedList.length).toBe(2) + }) + test("update 메서드로 두번째 노드의 값을 변경할 수 있다.", () => { + doublyLinkedList.update(1,10) + expect(doublyLinkedList.get(1).value).toBe(10) + }) +}); \ No newline at end of file diff --git "a/JavaScript/07\354\243\274\354\260\250/assignment/7\354\243\274\354\260\250/\353\257\274\355\230\234/package.json" "b/JavaScript/07\354\243\274\354\260\250/assignment/7\354\243\274\354\260\250/\353\257\274\355\230\234/package.json" new file mode 100644 index 0000000..a94f4d5 --- /dev/null +++ "b/JavaScript/07\354\243\274\354\260\250/assignment/7\354\243\274\354\260\250/\353\257\274\355\230\234/package.json" @@ -0,0 +1,10 @@ +{ + "main": "index.js", + "type": "module", + "scripts": { + "test": "jest" + }, + "devDependencies": { + "jest": "^29.3.1" + } +} From d8646fa4226a7c9b6f527a96f900ffe34419384e Mon Sep 17 00:00:00 2001 From: Minhye Kang Date: Tue, 6 Dec 2022 16:24:02 +0900 Subject: [PATCH 5/6] =?UTF-8?q?refactor:=20=EC=BD=94=EB=93=9C=20=EB=A6=AC?= =?UTF-8?q?=EB=B7=B0=20=EB=B0=98=EC=98=81=ED=95=98=EC=97=AC=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\353\257\274\355\230\234/index.js" | 55 ++++++++++++++----- 1 file changed, 40 insertions(+), 15 deletions(-) diff --git "a/JavaScript/07\354\243\274\354\260\250/assignment/7\354\243\274\354\260\250/\353\257\274\355\230\234/index.js" "b/JavaScript/07\354\243\274\354\260\250/assignment/7\354\243\274\354\260\250/\353\257\274\355\230\234/index.js" index 40c3f7c..7f99a65 100644 --- "a/JavaScript/07\354\243\274\354\260\250/assignment/7\354\243\274\354\260\250/\353\257\274\355\230\234/index.js" +++ "b/JavaScript/07\354\243\274\354\260\250/assignment/7\354\243\274\354\260\250/\353\257\274\355\230\234/index.js" @@ -1,7 +1,8 @@ /** * 양방향 연결리스트는 각 노드의 prev와 next로 이동할 수 있어야 한다. */ -class Node { + + class Node { constructor(value) { this.prev = null; this.value = value; @@ -15,12 +16,16 @@ class DoublyLinkedList { this.tail = null; this.length = 0; } + + checkindexValidation (index) { + if (index < 0 || index > this.length - 1) return true; + } /** * push를 통해 양방향 연결리스트 꼬리에 값을 추가할 수 있다. */ push(value) { - let newNode = new Node(value); + const newNode = new Node(value); if (!this.head) { this.head = newNode; this.tail = newNode; @@ -36,15 +41,17 @@ class DoublyLinkedList { * insert를 통해 특정 인덱스의 노드에 삽입할 수 있어야 한다. */ insert(index, value) { - if (index < 0 || index > index.length) return false; + if(this.checkindexValidation(index)) return false if (index === this.length - 1) return !!this.push(value); - let newNode = new Node(value); - let previousNode = this.get(index); - - newNode.next = previousNode.next; + const newNode = new Node(value); + const previousNode = this.get(index); + const afterNode = previousNode.next; + newNode.next = afterNode; + newNode.prev = previousNode; previousNode.next = newNode; + afterNode.prev = newNode; this.length++; return true; @@ -54,6 +61,7 @@ class DoublyLinkedList { * get을 통해 원하는 인덱스의 노드를 가져온다. */ get(index) { + if(this.checkindexValidation(index)) return false let counter = 0; let currentNode = this.head; while (counter < index) { @@ -68,20 +76,31 @@ class DoublyLinkedList { */ shift() { const shiftedHead = this.head; + + if(!this.head) return undefined; + if(this.length === 1){ + this.head = null; + this.tail = null; + } else{ this.head = shiftedHead.next; this.head.prev = null; + } this.length--; - return shiftedHead; + } /** * remove를 통해 원하는 인덱스의 노드를 삭제한다. */ remove(index) { - let currentNode = this.get(index); - let previousNode = currentNode.prev; - let afterNode = currentNode.next; + if (this.checkindexValidation(index)) return false; + if (index === 0) return this.shift(); + if (index === this.length-1) return this.pop(); + + const currentNode = this.get(index); + const previousNode = currentNode.prev; + const afterNode = currentNode.next; previousNode.next = afterNode; afterNode.prev = previousNode; @@ -95,9 +114,15 @@ class DoublyLinkedList { */ pop() { const poppedTail = this.tail; + + if(!this.head) return undefined; + if(this.length === 1){ + this.head = null; + this.tail = null; + } else{ this.tail = poppedTail.prev; this.tail.next = null; - + } this.length--; return poppedTail; @@ -107,9 +132,9 @@ class DoublyLinkedList { * update를 통해 원하는 인덱스의 노드를 수정할 수 있다. */ update(index, value) { - if (index < 0 || index > this.length) return false; + if(this.checkindexValidation(index)) return false - let currentNode = this.get(index); + const currentNode = this.get(index); currentNode.value = value; return true; @@ -118,4 +143,4 @@ class DoublyLinkedList { const list = new DoublyLinkedList(); -module.exports = DoublyLinkedList; +module.exports = DoublyLinkedList; \ No newline at end of file From 84bc9b47335468363796bdf2393bda0db6fd9a98 Mon Sep 17 00:00:00 2001 From: Minhye Kang Date: Tue, 6 Dec 2022 16:24:22 +0900 Subject: [PATCH 6/6] =?UTF-8?q?test:=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20prettier=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\353\257\274\355\230\234/index.test.js" | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git "a/JavaScript/07\354\243\274\354\260\250/assignment/7\354\243\274\354\260\250/\353\257\274\355\230\234/index.test.js" "b/JavaScript/07\354\243\274\354\260\250/assignment/7\354\243\274\354\260\250/\353\257\274\355\230\234/index.test.js" index 240a755..15c9766 100644 --- "a/JavaScript/07\354\243\274\354\260\250/assignment/7\354\243\274\354\260\250/\353\257\274\355\230\234/index.test.js" +++ "b/JavaScript/07\354\243\274\354\260\250/assignment/7\354\243\274\354\260\250/\353\257\274\355\230\234/index.test.js" @@ -1,4 +1,4 @@ -const DoublyLinkedList = require('./index'); +const DoublyLinkedList = require("./index"); describe("양방향 연결리스트", () => { let doublyLinkedList; @@ -11,36 +11,36 @@ describe("양방향 연결리스트", () => { expect(doublyLinkedList.length).toBe(3); }); test("1,2,3을 차례대로 추가했을 때 머리노드 값은 1이고 꼬리노드 값은 3이다.", () => { - expect(doublyLinkedList.head.value).toBe(1) - expect(doublyLinkedList.tail.value).toBe(3) - }) + expect(doublyLinkedList.head.value).toBe(1); + expect(doublyLinkedList.tail.value).toBe(3); + }); // 첫번째 머리노드(head)의 인덱스 번호는 0입니다. 두번째 노드의 인덱스 번호는 1입니다. test("get 메서드로 두번째 노드를 추출한 값은 2이다.", () => { const extractedNode = doublyLinkedList.get(1); expect(extractedNode.value).toBe(2); - }) - test("insert 메서드로 두번째 노드 뒤에 2.5를 추가할 수 있다.", () =>{ - doublyLinkedList.insert(1,2.5) - expect(doublyLinkedList.get(1).value).toBe(2) - expect(doublyLinkedList.get(2).value).toBe(2.5) - expect(doublyLinkedList.get(3).value).toBe(3) - expect(doublyLinkedList.length).toBe(4) - }) + }); + test("insert 메서드로 두번째 노드 뒤에 2.5를 추가할 수 있다.", () => { + doublyLinkedList.insert(1, 2.5); + expect(doublyLinkedList.get(1).value).toBe(2); + expect(doublyLinkedList.get(2).value).toBe(2.5); + expect(doublyLinkedList.get(3).value).toBe(3); + expect(doublyLinkedList.length).toBe(4); + }); test("remove 메서드로 원하는 인덱스의 값을 삭제할 수 있다.", () => { doublyLinkedList.remove(1); expect(doublyLinkedList.get(1).value).toBe(3); expect(doublyLinkedList.length).toBe(2); - }) + }); test("pop 메서드로 꼬리 노드를 꺼낼 수 있다.", () => { expect(doublyLinkedList.pop().value).toBe(3); expect(doublyLinkedList.length).toBe(2); - }) + }); test("shift 메서드로 머리 노드를 꺼낼 수 있다.", () => { expect(doublyLinkedList.shift().value).toBe(1); - expect(doublyLinkedList.length).toBe(2) - }) + expect(doublyLinkedList.length).toBe(2); + }); test("update 메서드로 두번째 노드의 값을 변경할 수 있다.", () => { - doublyLinkedList.update(1,10) - expect(doublyLinkedList.get(1).value).toBe(10) - }) -}); \ No newline at end of file + doublyLinkedList.update(1, 10); + expect(doublyLinkedList.get(1).value).toBe(10); + }); +});