gpt4 book ai didi

javascript - 从单链表中删除节点

转载 作者:行者123 更新时间:2023-11-29 14:40:15 26 4
gpt4 key购买 nike

我正在用 Javascript 为 LL 实现一个删除函数。

这是我的功能:

//Define Node obj
function Node(data){
this.data = data;
this.next = null;
}

//Define SinglyList obj
function SinglyList(){
this._length = 0;
this.head = null;
}

SinglyList.prototype.add = function(val){
var node = new Node(val),
currentNode = this.head;

//If empty, build as first node
if(!currentNode){
this.head = node;
this._length++;
return;
}

//iterate over until end of list
while(currentNode.next){
currentNode = currentNode.next;
}

//add/append new node
currentNode.next = node;
this._length++;

return node;
};

SinglyList.prototype.remove = function(index){
var currentNode = this.head, count=0, previous;
//if list is empty, exit out
if(this._length===0) return;

//Check if first node
if(index===0){
this.head = currentNode.next;
this._length--;
}else{

while(count<index){
previous = currentNode;
currentNode = currentNode.next;
count++;
}//end while

previous.next = currentNode.next;

return previous;
}// end if

};

var singlyList = new SinglyList();

singlyList.add(1);
singlyList.add(2);
singlyList.add(3);
singlyList.add(4);
singlyList.add(5);
singlyList.add(6);
singlyList.add(7);
singlyList.add(8);
singlyList.add(9);
singlyList.add(10);

console.log(JSON.stringify(singlyList));
console.log('Remove:\n'+JSON.stringify(singlyList.remove(5)));

问题:如果我的列表中有 10 个节点并调用此函数删除第 5 个节点,则此函数仅返回第 4-10 个节点,其中第 5 个节点被删除。但是,我希望它返回第 1-10 位,其中第 5 位被删除。我做错了什么以及如何检索仅删除第 5 个节点的列表?

最佳答案

静止你在代码中犯了一个小错误

1) while 循环应该运行到 < index-1 因为你从 0 开始计数

2) 你没有这样做。_length-- 在删除除第一个以外的节点之后

3) JSON.stringify 从 head 元素开始打印,当您删除节点时,您将返回前一个节点,因此您得到了错误的节点列表

修改后的代码在这里

//Define Node obj
function Node(data){
this.data = data;
this.next = null;
}

//Define SinglyList obj
function SinglyList(){
this._length = 0;
this.head = null;
}

SinglyList.prototype.add = function(val){
var node = new Node(val),
currentNode = this.head;

//If empty, build as first node
if(!currentNode){
this.head = node;
this._length++;
return;
}

//iterate over until end of list
while(currentNode.next){
currentNode = currentNode.next;
}

//add/append new node
currentNode.next = node;
this._length++;

return node;
};

SinglyList.prototype.remove = function(index){
var currentNode = this.head, count=0, previous;
//if empty, exit out
if(this._length===0) return;

//Check against first node
if(index===0){
this.head = currentNode.next;
this._length--;
}else{

while(count<index-1){
previous = currentNode;
currentNode = currentNode.next;
count++;
}//end while

previous.next = currentNode.next;
this._length--;

return this.head;
}// end if

};

var singlyList = new SinglyList();

singlyList.add(1);
singlyList.add(2);
singlyList.add(3);
singlyList.add(4);
singlyList.add(5);
singlyList.add(6);
singlyList.add(7);
singlyList.add(8);
singlyList.add(9);
singlyList.add(10);

document.write(JSON.stringify(singlyList));
singlyList.remove(5)
document.write('After removing 5 :\n'+JSON.stringify(singlyList));

关于javascript - 从单链表中删除节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39283662/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com