단일 연결 목록

4079 단어 hacktoberfest
클래스 노드{

생성자(발){

this.val = val;

this.next = null; 

}
}

클래스 SinglylinkedList{

건설자(){

this.head = null;

this.tail = null;

this.length = 0;

}

//목록에 새 값 추가

푸시(발){

var newNode = new Node(val);

if(!this.head){

    this.head = newNode;

    this.tail = this.head;

} else {

    this.tail.next = newNode;

    this.tail = newNode;

}

this.length++;

console.log(` Your ${this.length} added value is --> ${val}`);

return this;

}

//모든 목록 표시

횡단(){

var current = this.head;

while(current){

    console.log(current.val)

    current = current.next;

}

}

//목록에서 끝 값 삭제

팝(){

if(!this.head) return undefined;

var current = this.head;

var newTail = current;

while(current.next){

    newTail = current;

    current = current.next;

}

this.tail = newTail;

this.tail.next = null;

this.length--;

    if(this.length === 0){

        this.head = null;

        this.tail = null;

    }

    console.log(`${current.val} <- This value is deleted`)

    console.log(`${newTail.val} <- This is a Next value`)

return current;

}

//목록에서 헤드 값 삭제

옮기다(){

if(!this.head) return undefined;

var currentHead = this.head;

this.head = currentHead.next;

this.length--;

return currentHead;

if(this.length === 0){

    this.tail = null;

}

}

//목록의 헤드에 값 추가

언시프트(발){

var newNode = new Node(val)

if(!this.head){

    this.head = newNode;

    this.tail = this.head;

} else { 

    newNode.next = this.head;

    this.head = newNode;

}

this.length++;

return this;

}

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

가져오기(색인){

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

var counter = 0;

var current = this.head;

while(counter !==  index){

    current = current.next;

    counter++;

}

return current;

}

//노드의 값 변경

세트(인덱스, 발){

var foundNode = this.get(index);

if(foundNode){

    foundNode.val = val;

    return true;

}

return false;

}

//노드 삽입

삽입(인덱스, 발){

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

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

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

var newNode = new Node(val);

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

var temp = prev.next;

prev.next = newNode;

newNode.next = temp;

this.length++;

console.log(` You are added no.${this.length} -> The value is -> ${val}`)

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 previousNode = this.get(index -1);

var remove = previousNode.next;

this.length--;

return remove;

}

//역 노드 값 출력

뒤집다(){

var node = this.head;

this.head = this.tail;

this.tail = node

var next;

var prev = null;

for(var i = 0; i < this.length; i++){

    next = node.next;

    node.next = prev;

    prev =  node;

    node = next;

}

return this

}

//모든 값을 출력

인쇄(){

var arr = [];

var current = this.head

while(current){

   arr.push(current.val)

   current = current.next 

}

console.log(arr);

}
}

var 목록 = 새로운 SinglelinkedList()

list.push("자바")

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

list.push("HTML")

list.push("Css")

좋은 웹페이지 즐겨찾기