我正在尝试在链接列表的第五个位置插入一个项目。当我测试代码时,它什么也没做。我尝试过使用调试器来解决这个问题,但似乎无法弄清楚。
template <typename T>
void LinkedList<T>::insertNewFifthElement(const T& value) {
int counter = 1;
//New node to insert
Node<T>* node = new Node<T>;
//Value inserted into new node
node->data = value;
//Pointers
Node<T>* temp = this->first;
Node<T>* temp2 = node;
while (counter != 4) {
temp = temp->link;
counter++;
}
if (counter == 4) {
temp2->link = temp;
temp->link = node;
}
}
答案 0 :(得分:0)
您设置了循环链接,而不是将节点插入列表中。
if (counter == 4) {
node->link = temp->link; //< note!
temp->link = node;
}