이중 연결 목록

3976 단어 hacktoberfest
클래스 노드 {

생성자(발) {

this.val = val;

this.next = null;

this.prev = null; 

}
}

클래스 DoublelinkList {

생성자() {

this.head = null;

this.tail = null;

this.length = 0;

}
//👉👉 목록에 새 값 추가

푸시(발) {

var 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++;

console.log( ` You are added ${this.length} Node ->> ${val}` );

return this;

}

//👉👉 노드에서 종료 값 삭제

팝() {

if(!this.head) return undefined;

var poppedNode = this.tail;

if(this.length === 1) {

    this.head = null;

    this.tail = null;

} else {

    this.tail = poppedNode.prev;

}

this.length--;

console.log(` Deleted Successfully 👍`)

return poppedNode;

}

//👉👉 목록에서 헤드 값 삭제

옮기다() {

if(!this.head) return undefined;

var oldHead = this.head;

if(this.length === 1) {

    this.head = null;

    this.tail = null;

} else {

    this.head = oldHead.next;

    this.head.prev = null;

    oldHead.next = null;   

}

this.length--;

return oldHead;

}

//👉👉 목록의 헤드에 값 추가

언시프트(발) {

var newNode = new Node(val);

if(this.length === 0) {

    this.head = newNode;

    this.tail = newNode;

} else {

    this.head.prev = newNode

    newNode.next = this.head;

    this.head = newNode;

}

this.length++;

console.log(" -> Added Successfully 👍" );

return this;

}

//노드 번호를 쓰고

//이 번호를 표시합니다. 노드 이름

get(인덱스) {

if(index < 0 || index >= this.length) return null;

if(index <= this.length/2){

    var count = 0;

    var current = this.head;

    while(count != index){

        current = current.next;

        count++;

    }

    return current;

} else {

    var count = this.length - 1;

    var current = this.tail;

    while(count !== index){

        current = current.next;

        count++;

    }

    return current;

}

console.log(" -> Finded Successfully 👍" );

}

//노드의 값 변경

세트(인덱스, 발) {

var foundNode = this.get(index);

if(foundNode != null) {

    foundNode.val = val;

    return true;

}

return false;

}

//노드 삽입

삽입(인덱스, 발) {

if(index < 0 || index > this.length) return false;

if(index === 0) return this.unShift(val);

if(index === this.length) return this.push(val);

var newNode = new Node(val);

var beforeNode = this.get(index - 1);

var afterNode = beforeNode.next;

beforeNode.next = newNode;

newNode.prev = beforeNode;

newNode.next = afterNode;

afterNode.prev = newNode;

this.length++;

return true;

}

//노드 제거

제거(색인) {

if(index < 0 || index >= this.length) return undefined;

if(index === 0) return this.Shift()

if(index === this.length -1) return this.pop();

var removeNode = this.get(index);

var beforeNode = removeNode.prev;

var afterNode = removeNode.next;

beforeNode.next = afterNode;

afterNode.prev = beforeNode;

//removeNode.prev.next = removeNode.next;

//removeNode.next.next = removeNode.prev;

removeNode.next = null;

removeNode.prev = null;

this.length--;

return removeNode;

}
}

var 목록 = 새로운 DoublelinkList()

list.push("자바")

list.push("자바 스크립트")

list.push("HTML")

list.push("Css")

좋은 웹페이지 즐겨찾기