所以我是 C++ 的初学者,我有一个学校项目来创建一个链表,我现在正在研究合并方法,我不确定为什么它不起作用。我发现问题出在第二个 if 在 while 循环中,而不是更改 head_ list 节点,而是更改 list1 列表,我不知道它为什么这样做
template <typename T>
bool List342<T>::Merge(const List342<T>& list1) {
if (head_ == nullptr) {
head_ = list1.head_;
return true;
}
if (list1.head_ == nullptr) {
return false;
}
Node<T>* l1_ptr = list1.head_;
Node<T>* head_ptr = head_;
while (l1_ptr != nullptr && head_ptr != nullptr) {
if (*head_ptr->data == *l1_ptr->data) {
l1_ptr = l1_ptr->next;
head_ptr = head_ptr->next;
}
else if (*head_ptr->data <= *l1_ptr->data) {
Node<T>* temp = head_ptr->next;
head_ptr->next = l1_ptr;
l1_ptr->next = temp;
l1_ptr = l1_ptr->next;
head_ptr = head_ptr->next;
}
}
return true;
}