Skip to content
Open
206 changes: 206 additions & 0 deletions 19_singly-linked-lists/1_chaewon.js
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그렇군요 노드가 한개만 있을땐 이런식으로 처리되어야 하네요! 저는 놓쳤어용

차카니 방식도 좋구
아래 코드로 바꿔서 노드 길이가 0일때, 1일때, 그외 이렇게 적는것도 가독성 좋아보여~

 if(this.length === 1) {
        this.head = null;
        this.tail = null;
        this.length--;
        return current;  
    }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

근데 다른 매서드도 다 보고나니까 차카니 방식이 코드도 짧고 이해하기도 쉽고 내 말 무시해용가리

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그러게용
저도 후반에는 this.length가 1인 경우에 대해 앞에서 처리했던 것 같은데
초반에는 빼낸 다음에 처리했네

저도 용가링말대루 앞에서 하는게 조은 것 같아여

{
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 ++;
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 removed 20_doubly-linked-lists/.gitkeep
Empty file.
89 changes: 89 additions & 0 deletions 20_doubly-linked-lists/1_chaewon.js
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
54 changes: 54 additions & 0 deletions 20_doubly-linked-lists/2_chaewon.js
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
Loading