Javascript 链表-在索引处查找第n个元素
在链表的特定索引中查找元素的代码有问题Javascript 链表-在索引处查找第n个元素,javascript,linked-list,Javascript,Linked List,在链表的特定索引中查找元素的代码有问题 findElement(index) { let currentNode = this.head; let count = 0; while (currentNode != null) { if (count === index) { return currentNode; count++; currentNode = currentNode.next;
findElement(index) {
let currentNode = this.head;
let count = 0;
while (currentNode != null) {
if (count === index) {
return currentNode;
count++;
currentNode = currentNode.next;
}
return -1;
}
}
当我这样做时,我得到的是整个链表,而不是一个特定的节点。因此,如果我使用console.log(list.findElement(0)),我将得到整个链表。但是如果我控制台log console.log(list.findElement(1)),我得到-1。但我想要的是第二个节点。下面是我的代码的其余部分。不完全确定我的findElement函数有什么问题
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
class LinkedList {
constructor() {
this.head = null;
//Length
this.length = 0;
}
//Push-front
pushFront(value) {
let node = new Node(value);
node.next = this.head;
this.head = node;
this.length++;
}
//Pop-front
popFront() {
if (this.head != null) {
this.head = this.head.next;
}
this.length--;
}
//Push-back
pushBack(value) {
let node = new Node(value);
if (this.head === null) {
this.head = node;
} else {
let currentNode = this.head;
while (currentNode.next) {
currentNode = currentNode.next;
}
currentNode.next = node;
}
this.length++;
}
您有一个
return
语句作为if(count==index)
条件的第一行,它阻止执行进一步的代码(意思是currentNode=currentNode.next
)
您希望将return currentNode
向下移动两行,以便在函数返回之前引用后续节点
findElement(index) {
let currentNode = this.head;
let count = 0;
while (currentNode != null) {
if (count === index) {
count++;
currentNode = currentNode.next;
return currentNode;
}
return -1;
}
}
findElement
函数中的逻辑存在一些问题。主要问题是count
从不从0更改,因此该函数仅在头是所搜索的元素(例如index==0
)并且在任何其他输入上返回-1
(该“fail”返回应该在循环完全执行时移到之外)时才起作用
这是一个带有count++
和currentNode=currentNode的版本。接下来
从if
移动到隐式else
:
findElement(index) {
let currentNode = this.head;
let count = 0;
while (currentNode) {
if (count === index) { // found the element
return currentNode;
}
count++; // increment counter
currentNode = currentNode.next; // move to next node
}
return -1;
}
另一个问题是,如果在空列表中调用,则您的popFront
会将列表长度减少到-1
。减量应该是有条件的,也应该是有条件的。这可能会在将来的实现中造成危害,但由于您从不使用列表长度,因此可以完全删除它
总而言之,这里有一个测试程序:
类节点{
构造函数(值){
这个值=值;
this.next=null;
}
}
类链接列表{
构造函数(){
this.head=null;
这个长度=0;
}
findElement(索引){
让currentNode=this.head;
让计数=0;
while(当前节点){
如果(计数===索引){
返回当前节点;
}
计数++;
currentNode=currentNode.next;
}
返回-1;
}
推前(值){
让节点=新节点(值);
node.next=this.head;
this.head=节点;
这个.length++;
}
popFront(){
如果(this.head!=null){
this.head=this.head.next;
这个长度--;
}
}
回推(值){
让节点=新节点(值);
if(this.head==null){
this.head=节点;
}
否则{
让currentNode=this.head;
while(currentNode.next){
currentNode=currentNode.next;
}
currentNode.next=节点;
}
这个.length++;
}
}
const ll=new LinkedList();
ll.推回(1);
ll.推回(2);
ll.推回(3);
控制台日志(ll);
log(`First node:${ll.findElement(0.value}`);
log(`Second node:${ll.findElement(1.value}`);
log(`Third node:${ll.findElement(2.value}`);
log(`Invalid index:${ll.findElement(22.value}`)代码>更改后,console.log(list.findElement(0))的结果是第二个节点,而不是第一个节点。我一定是在别的地方做错了什么。这不是什么大的进步count
和currentNode
在循环中不会更改<代码>-1
仍在循环中,因此如果第一个元素不匹配,循环将立即返回-1
。