November 12, 2020
array[3] = null
과 같은 방법으로 정적이게 할수도 있다 한다.C
JavaScript, Python
JAVA
Array
index 3인 위치에 값을 추가한다고 가정해보았을 때,
Array
List
index 3인 위치에 값을 제거한다고 가정해보았을 때,
Array
List
[1,2,3]
이전에, Kotlin으로 Loa-Hands를 할 때, Recycler View를 사용했었는데, 이 때에는 index를 조회하여 View를 구성하고, 초기에 List를 지정해주면 변할일이 없기 때문에, ArrayList가 적합했다.
처음을 알아야 예상한대로 다음으로 넘어갈수 있기 때문
class LinkedList {
constructor(value){
this.head = {
value : value,
next : null,
prev : null,
}
this.tail = this.head;
this.cur = this.head;
this.length = 1;
}
append(value){
const newNode = {
value : value,
next : null,
prev : this.tail
}
this.tail.next = newNode;
this.tail = newNode;
this.length++;
}
preAppend(value){
const newNode = {
value : value,
next : this.head,
prev : null
}
this.head.prev = newNode;
this.head = newNode;
this.length++;
}
shift(){
this.head = this.head.next;
this.head.prev = null;
this.length--;
}
pop(){
this.tail = this.tail.prev;
this.tail.next = null;
this.length--;
}
insert(index, value){
this.cur = this.head;
if(index >= this.length){
this.append(value);
}else if(index === 0){
this.prAppend(value);
}else{
let count = 1;
while(count !== index){
this.cur = this.cur.next;
count++;
}
const newNode = {
value : value,
prev : this.cur,
next : this.cur.next
}
this.cur.next = newNode;
newNode.next.prev = newNode;
this.length++;
}
}
delete(index){
const half = Math.ceil(this.length/2);
if(index === 0){
this.shift()
}else if(index === this.length-1){
this.pop()
}else{
if(index <= half){
this.cur = this.head;
let count = 1;
while(count !== index){
this.cur = this.cur.next;
count++;
}
this.cur.next = this.cur.next.next;
this.cur.next.prev = this.cur;
}else{
this.cur = this.tail;
let count = this.length;
while(count !== index){
this.cur = this.cur.prev;
count--;
}
this.cur.next = this.cur.next.next;
this.cur.next.prev = this.cur;
}
this.length--;
}
}
getArray(){
this.cur = this.head;
let
count = 0,
array = [];
while(count !== this.length){
array.push(this.cur.value);
this.cur = this.cur.next;
count++;
}
return array;
}
}
linkedList = new LinkedList(1);
linkedList.append(2)
linkedList.append(4)
linkedList.append(5)
linkedList.append(6)
linkedList.insert(2,3)
linkedList.append(7)
linkedList.delete(2)
console.log(linkedList)