我想使用add ()
方法按索引插入节点,但它无法帮助修复它。
这就是通话的样子:
list.add ('2', 1); // At the list: '1', '2', '3'
但是当我拨打电话时会触发错误:
throw new Error("Position Node Doesnt Exist!")
我的所有代码:
class Node {
constructor(value) {
this.value = value; // value in the node
this.next = null; // link to the next node
}
}
class SinglyLinkedList {
constructor() {
/*pointer to the knot-head
(first element) of the list*/
this.head = null;
/*pointer to the node-tail
(last item) of the list*/
this.tail = null;
this.length = 0;
}
//insert to the beginning (returns the inserted value)
addFirst(value) {
//create a new Node
let newNode = new Node(value);
//Handle case for when linked list is not empty
if (this.head) {
newNode.next = this.head;
//this.head = newNode;
} else {//Linked list is empty
this.tail = newNode;
//this.head = newNode
}
//set the head to the new node
this.head = newNode;
//increment count
this.length++;
}
//insert to the end(returns the inserted value)
addLast(value) {
//create a new Node
let newNode = new Node(value);
//Handle case for when linked list is not empty
if (this.tail) {
this.tail.next = newNode;
//this.tail = newNode;
} else {
this.head = newNode;
//this.tail = newNode
}
this.tail = newNode;
//increment count
this.length++;
}
findIndex(item) {
let currentNode = this.head;
while (currentNode) {
if (currentNode.value === item) {
return currentNode;
}
currentNode = currentNode.next;
}
return null;
}
add(value, index) {
//create the new node based on the name passed
let newNode = new Node(value);
//find the position or item node we want to insert after.
let positionNode = this.findIndex(index);
if (positionNode != null) {
//first set the next pointer of new node to be that of position nodes next
newNode.next = positionNode.next;
//finally update the positionNode's next to be the new node
positionNode.next = newNode;
this.length++;
} else {
//position not found, return error
throw new Error("Position Node Doesnt Exist!")
}
}
}
let list = new SinglyLinkedList();
list.addFirst('1');
list.addLast('3');
list.add('2', 1);
console.log(list);

答案 0 :(得分:2)
您正尝试将字符串与findIndex
方法中的数字进行比较:
if (currentNode.value === item) {
要允许自动类型强制,您可以使用双等运算符。或者您可以将两个值转换为相同的类型(数字或字符串)
if (currentNode.value == item) {
请在下面找到已修改的工作代码段。
class Node {
constructor(value) {
this.value = value; // value in the node
this.next = null; // link to the next node
}
}
class SinglyLinkedList {
constructor() {
/*pointer to the knot-head
(first element) of the list*/
this.head = null;
/*pointer to the node-tail
(last item) of the list*/
this.tail = null;
this.length = 0;
}
//insert to the beginning (returns the inserted value)
addFirst(value) {
//create a new Node
let newNode = new Node(value);
//Handle case for when linked list is not empty
if (this.head) {
newNode.next = this.head;
//this.head = newNode;
} else {//Linked list is empty
this.tail = newNode;
//this.head = newNode
}
//set the head to the new node
this.head = newNode;
//increment count
this.length++;
}
//insert to the end(returns the inserted value)
addLast(value) {
//create a new Node
let newNode = new Node(value);
//Handle case for when linked list is not empty
if (this.tail) {
this.tail.next = newNode;
//this.tail = newNode;
} else {
this.head = newNode;
//this.tail = newNode
}
this.tail = newNode;
//increment count
this.length++;
}
findIndex(item) {
let currentNode = this.head;
while (currentNode) {
if (currentNode.value == item) {
return currentNode;
}
currentNode = currentNode.next;
}
return null;
}
add(value, index) {
//create the new node based on the name passed
let newNode = new Node(value);
//find the position or item node we want to insert after.
debugger;
let positionNode = this.findIndex(index);
if (positionNode != null) {
//first set the next pointer of new node to be that of position nodes next
newNode.next = positionNode.next;
//finally update the positionNode's next to be the new node
positionNode.next = newNode;
this.length++;
} else {
//position not found, return error
throw new Error("Position Node Doesnt Exist!")
}
}
}
let list = new SinglyLinkedList();
list.addFirst('1');
list.addLast('3');
list.add('2', 1);
console.log(list);
答案 1 :(得分:1)
您可以在findIndex
内跟踪您所在的索引,而不是比较值。
class Node {
constructor(value) {
this.value = value; // value in the node
this.next = null; // link to the next node
}
}
class SinglyLinkedList {
constructor() {
/*pointer to the knot-head
(first element) of the list*/
this.head = null;
/*pointer to the node-tail
(last item) of the list*/
this.tail = null;
this.length = 0;
}
//insert to the beginning (returns the inserted value)
addFirst(value) {
//create a new Node
let newNode = new Node(value);
//Handle case for when linked list is not empty
if (this.head) {
newNode.next = this.head;
//this.head = newNode;
} else {//Linked list is empty
this.tail = newNode;
//this.head = newNode
}
//set the head to the new node
this.head = newNode;
//increment count
this.length++;
}
//insert to the end(returns the inserted value)
addLast(value) {
//create a new Node
let newNode = new Node(value);
//Handle case for when linked list is not empty
if (this.tail) {
this.tail.next = newNode;
//this.tail = newNode;
} else {
this.head = newNode;
//this.tail = newNode
}
this.tail = newNode;
//increment count
this.length++;
}
findIndex(item) {
let currentNode = this.head;
let index = 0;
while (currentNode) {
if (index === item - 1) {
return currentNode;
}
index++;
currentNode = currentNode.next;
}
return null;
}
add(value, index) {
//create the new node based on the name passed
let newNode = new Node(value);
//find the position or item node we want to insert after.
let positionNode = this.findIndex(index);
if (positionNode != null) {
//first set the next pointer of new node to be that of position nodes next
newNode.next = positionNode.next;
//finally update the positionNode's next to be the new node
positionNode.next = newNode;
this.length++;
} else {
//position not found, return error
throw new Error("Position Node Doesnt Exist!")
}
}
}
let list = new SinglyLinkedList();
list.addFirst('1');
list.addLast('3');
list.add('2', 1);
console.log(list);
答案 2 :(得分:1)
您的逻辑似乎有误。 findindex函数查找具有某个值的项。 你用索引作为参数调用它......这是错误的。 您需要一个index为参数的findItem方法
if(index<0||index>length) {return null or head or tail...}
let currentNode = this.head;
let currentIndex=0;
while (currentIndex<index) {
currentNode = currentNode.next;
CurrentIndex++;
}
return currentNode;