-
Notifications
You must be signed in to change notification settings - Fork 0
[19-20] 문제풀이/chaewon #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
icw0201
wants to merge
12
commits into
main
Choose a base branch
from
19_chaewon
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
04ae9d3
[19] 문제풀이/chaewon
icw0201 5d2395a
[20]-1 문제풀이/chaewon
icw0201 8bc62d8
[20]-2 문제풀이/chaewon
icw0201 49b3b0f
[20]-3 문제풀이/chaewon
icw0201 3c34adf
[20]-4 문제풀이/chaewon
icw0201 1fe9a81
[20]-5 문제풀이/chaewon
icw0201 28478a1
[20]-6 문제풀이/chaewon
icw0201 d8bab02
[20] 문제풀이/chaewon
icw0201 32befa2
[20]-5 문제풀이/chaewon/오류 수정정
icw0201 dab0a40
[20]-8 문제풀이/chaewon
icw0201 ce78c08
[20]-9 문제풀이/chaewon
icw0201 0c38829
[19] 문제풀이/chaewon/피드백 수정
icw0201 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
//연결 리스트 구현하기 | ||
|
||
//노드 클래스 | ||
//연결 리스트 클래스 | ||
//push, pop, shift, unshift, get, set, insert, remove, reverse 함수 구현하기 | ||
|
||
class Node | ||
{ | ||
constructor(val) | ||
{ | ||
this.val = val; | ||
this.next = null; | ||
} | ||
} | ||
|
||
class SingleLinkedList | ||
{ | ||
constructor() | ||
{ | ||
this.head = null; | ||
this.tail = null; | ||
this.length = 0; | ||
} | ||
//리스트 맨 끝에 원소 추가 | ||
push(val) | ||
{ | ||
let newNode = new Node(val); | ||
//빈 리스트일 경우 | ||
if(!this.head) | ||
{ | ||
this.head = newNode; | ||
this.tail = newNode; | ||
} | ||
//일반적인 경우 | ||
else | ||
{ | ||
this.tail.next = newNode; | ||
this.tail = newNode; | ||
} | ||
this.length++; | ||
return true; | ||
} | ||
//리스트 맨 끝 원소 꺼내기 | ||
pop() | ||
{ | ||
if(this.length === 0) return undefined; | ||
|
||
//tail의 원소를 꺼내고 기존 -2 원소를 tail로 지정 | ||
let current = this.head; | ||
let newTail = current; | ||
while(current.next) | ||
{ | ||
newTail = current; | ||
current = current.next; | ||
} | ||
|
||
this.tail = newTail; | ||
newTail.next = null; | ||
this.length--; | ||
if(this.length === 0) | ||
{ | ||
this.head = null; | ||
this.tail = null; | ||
} | ||
return current; | ||
} | ||
//head에서 pop | ||
shift() | ||
{ | ||
//0번이 return되고 1번이 새 head가 된다 | ||
if(this.length === 0) return undefined; | ||
|
||
let current = this.head; | ||
this.head = this.head.next; | ||
|
||
this.length--; | ||
|
||
if(this.length === 0) this.tail = null; | ||
current.next = null; | ||
return current; | ||
} | ||
//head에 추가 | ||
unshift(val) | ||
{ | ||
//val이 새 헤드가 되고, val.next에 기존 head를 연결한다 | ||
let newNode = new Node(val); | ||
//리스트가 빈 경우 | ||
if(this.length === 0) | ||
{ | ||
this.tail = newNode; | ||
} | ||
//일반적인 경우 | ||
else | ||
{ | ||
newNode.next = this.head; | ||
} | ||
this.head = newNode; | ||
this.length ++; | ||
icw0201 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return true; | ||
} | ||
//해당 index에 있는 원소를 반환 | ||
get(index) | ||
{ | ||
if(index > this.length) return false; | ||
let count = 0; | ||
let current = this.head; | ||
while(count !== index) | ||
{ | ||
current = current.next; | ||
count ++; | ||
} | ||
|
||
return current; | ||
} | ||
//해당 index에 있는 val 수정 | ||
set(index, val) | ||
{ | ||
if(index > this.length) return false; | ||
let current = this.get(index); | ||
if(!current) return null; | ||
current.val = val; | ||
return true; | ||
} | ||
//해당 index에 val 추가 | ||
insert(index, val) | ||
{ | ||
let newNode = new Node(val); | ||
|
||
if(index > this.length) return false; | ||
if(index === this.length) return !!this.push(val); //느낌표 두개로 bool로 반환 | ||
if(index === 0) return !!this.unshift(val); | ||
|
||
//index -1의 next로 새 노드 지정, 새 노드의 next로 그 다음 노드 | ||
let preNode = this.get(index - 1); | ||
let nextNode = preNode.next; | ||
preNode.next = newNode; | ||
newNode.next = nextNode; | ||
this.length++; | ||
|
||
return true; | ||
} | ||
//해당 index의 원소 제거 | ||
remove(index) | ||
{ | ||
//원소 제거하면서 next도 잘... | ||
if(index >= this.length) return false; | ||
if(index === 0 ) return !!this.shift(); | ||
if(index === this.length - 1) return !!this.pop(); | ||
|
||
let preNode = this.get(index - 1); | ||
preNode.next = preNode.next.next; | ||
|
||
return true; | ||
} | ||
//뒤집기!! | ||
reverse() | ||
{ | ||
let current = this.head; | ||
this.head = this.tail; | ||
this.tail = current; | ||
|
||
let preNode = null; | ||
let nextNode = null; | ||
while(current) | ||
{ | ||
nextNode = current.next; | ||
current.next = preNode; | ||
preNode = current; | ||
current = nextNode; | ||
} | ||
return this; | ||
} | ||
//원소 프린트 | ||
print() | ||
{ | ||
let current = this.head; | ||
let res = [] | ||
//head부터 next가 없을 때까지 | ||
while(current) | ||
{ | ||
res.push(current.val); | ||
current = current.next; | ||
} | ||
console.log(res); | ||
} | ||
} | ||
|
||
let list = new SingleLinkedList(); | ||
list.push(5); | ||
list.push(10); | ||
list.push(15); | ||
list.push(20); | ||
console.log(list.pop()); //20 | ||
console.log(list.shift()); //5 | ||
list.unshift(5); | ||
list.print(); //5 10 15 | ||
console.log(list.get(2)); //15 | ||
list.set(2, 20); | ||
list.print(); //5 10 20 | ||
list.insert(2, 15); | ||
list.insert(4, 25); | ||
list.print(); //5 10 15 20 25 | ||
list.remove(3); | ||
list.print(); //5 10 15 25 | ||
list.reverse(); | ||
list.print(); //25 15 10 5 |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
//더블 링크드 리스트의 push&pop 구현하기 | ||
//push하고는 this를 반환해야 함 | ||
|
||
class Node | ||
{ | ||
constructor(val) | ||
{ | ||
this.val = val; | ||
this.next = null; | ||
this.prev = null; | ||
} | ||
} | ||
|
||
class DoublyLinkedList | ||
{ | ||
constructor() | ||
{ | ||
this.head = null; | ||
this.tail = null; | ||
this.length = 0; | ||
} | ||
//push | ||
push(val) | ||
{ | ||
let newNode = new Node(val); | ||
if(this.length === 0) | ||
{ | ||
this.head = newNode; | ||
this.tail = newNode; | ||
} | ||
else | ||
{ | ||
this.tail.next = newNode; | ||
newNode.prev = this.tail; | ||
this.tail = newNode; | ||
} | ||
this.length++; | ||
return this; | ||
} | ||
pop() | ||
{ | ||
if(this.length === 0) return undefined; | ||
let res = this.tail; | ||
if(this.length === 1) | ||
{ | ||
this.head = null; | ||
this.tail = null; | ||
} | ||
else | ||
{ | ||
this.tail = res.prev; | ||
this.tail.next = null; | ||
} | ||
res.prev = null; | ||
|
||
this.length --; | ||
return res; | ||
} | ||
} | ||
|
||
|
||
|
||
var doublyLinkedList = new DoublyLinkedList; | ||
console.log(doublyLinkedList.push(5)); // doublyLinkedList | ||
console.log(doublyLinkedList.length); // 1 | ||
console.log(doublyLinkedList.head.val); // 5 | ||
console.log(doublyLinkedList.tail.val); // 5 | ||
console.log(doublyLinkedList.head.prev); // null | ||
console.log(doublyLinkedList.push(10)); | ||
console.log(doublyLinkedList.length); // 2 | ||
console.log(doublyLinkedList.head.val); // 5 | ||
console.log(doublyLinkedList.head.next.val); // 10 | ||
console.log(doublyLinkedList.tail.val); // 10 | ||
console.log(doublyLinkedList.head.next.prev.val); // 10 | ||
console.log(doublyLinkedList.push(15)); | ||
console.log(doublyLinkedList.length); // 3 | ||
console.log(doublyLinkedList.head.val); // 5 | ||
console.log(doublyLinkedList.tail.val); // 15 | ||
console.log(doublyLinkedList.tail.prev.val); // 10 | ||
console.log(doublyLinkedList.head.next.next.val); // 15 | ||
|
||
console.log(doublyLinkedList.pop().val); // 15 | ||
console.log(doublyLinkedList.length); // 2 | ||
console.log(doublyLinkedList.pop().val); // 10 | ||
console.log(doublyLinkedList.length); // 1 | ||
console.log(doublyLinkedList.pop().val); // 5 | ||
console.log(doublyLinkedList.length); // 0 | ||
console.log(doublyLinkedList.pop()); // undefined | ||
console.log(doublyLinkedList.length); // 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
//시작에 새 원소를 추가하는 unshift 구현. retrun은 this | ||
class Node | ||
{ | ||
constructor(val) | ||
{ | ||
this.val = val; | ||
this.next = null; | ||
this.prev = null; | ||
} | ||
} | ||
|
||
class DoublyLinkedList | ||
{ | ||
constructor() | ||
{ | ||
this.head = null; | ||
this.tail = null; | ||
this.length = 0; | ||
} | ||
//시작에 추가하기 | ||
unshift(val) | ||
{ | ||
let newNode = new Node(val); | ||
if(this.length === 0) | ||
{ | ||
this.head = newNode; | ||
this.tail = newNode; | ||
} | ||
else | ||
{ | ||
newNode.next = this.head; | ||
this.head.prev = newNode; | ||
this.head = newNode; | ||
} | ||
this.length++; | ||
return this; | ||
} | ||
} | ||
|
||
var doublyLinkedList = new DoublyLinkedList; | ||
console.log(doublyLinkedList.unshift(5)); // doublyLinkedList | ||
console.log(doublyLinkedList.length); // 1 | ||
console.log(doublyLinkedList.head.val); // 5 | ||
console.log(doublyLinkedList.tail.val); // 5 | ||
console.log(doublyLinkedList.unshift(10)); // doublyLinkedList | ||
console.log(doublyLinkedList.length); // 2 | ||
console.log(doublyLinkedList.head.val); // 10 | ||
console.log(doublyLinkedList.head.next.val); // 5 | ||
console.log(doublyLinkedList.tail.val); // 5 | ||
console.log(doublyLinkedList.unshift(15)); // doublyLinkedList | ||
console.log(doublyLinkedList.length); // 3 | ||
console.log(doublyLinkedList.head.val); // 15 | ||
console.log(doublyLinkedList.tail.val); // 5 | ||
console.log(doublyLinkedList.head.next.next.val); // 5 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
그렇군요 노드가 한개만 있을땐 이런식으로 처리되어야 하네요! 저는 놓쳤어용
차카니 방식도 좋구
아래 코드로 바꿔서 노드 길이가 0일때, 1일때, 그외 이렇게 적는것도 가독성 좋아보여~
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
근데 다른 매서드도 다 보고나니까 차카니 방식이 코드도 짧고 이해하기도 쉽고 내 말 무시해용가리
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
그러게용
저도 후반에는 this.length가 1인 경우에 대해 앞에서 처리했던 것 같은데
초반에는 빼낸 다음에 처리했네
저도 용가링말대루 앞에서 하는게 조은 것 같아여