c++ - 如何在循环双向链表中使用指针
问题描述
我无法让编译器在 while 循环中停止循环。对于此功能,有两条链,您应该将它们编织在一起。例如,链 1 有一个头哨兵节点,然后是 1,2,3,4。链 2 有一个头部哨兵,然后是 5、6、7、8。输出应该是 Chain1 - head,1,5,2,6,3,7,4,8 并且 Chain 2 只有 head。一个空链仍然有一个头节点。如果一条链比另一条短,则其余节点应将自身附加到 Chain1,因为输出始终为 Chain1。
我一直在调整东西指向的地方,以前是反复打印出问题2,现在是反复打印出问题4。我想我并没有指出我的指针应该去哪里,但我什至不确定它们现在指向什么。
void Chain::weave(Chain & other) {
if(other.height_ != this->height_ || other.width_ != width_){
cout << "Block sizes differ." << endl;
} else if (other.size() == 0){
return;
}else if (this->size() == 0 && other.size() >= 1){
this->head_->next = other.head_->next;
other.head_->next->prev = this->head_;
this->head_->prev = other.head_->prev;
other.head_->prev->next = this->head_;
} else {
Node * current = head_->next;
Node * othernode = other.head_->next;
Node * pre;
Node * nex;
Node * opre;
Node * onex;
while (current != nullptr || othernode !=nullptr){
if (current != nullptr && othernode !=nullptr){
current = current->next;
othernode = othernode->next;
opre = othernode->prev;
onex = other.head_->next->next;
pre = current->prev;
nex = current->next;
current->next = othernode;
othernode->prev = current;
nex->prev = othernode;
othernode->next = nex;
current = nex;
othernode = onex;
cout << this->size() << endl;
cout << other.size() << endl;
cout << "problem1" << endl;
if(onex == other.head_){
othernode = nullptr;
cout << "problem2" << endl;
}
if (current == this->head_){
current = nullptr;
cout << "problem3" << endl;
}
}else if(current == nullptr && othernode != nullptr){
this->head_->next = othernode;
cout << "problem4" << endl;
}
}
}
}
解决方案
这一行,就在你打印“问题 4”之前:
this->head_->next = othernode;
应该
pre->next->next = othernode;
break;
您不想更改,head_
除非列表为空。但你之前检查过。所以你知道代码中的这个列表不是空的。
您要更改的节点是之前的节点,也就是那个current
节点pre->next
。pre->next->next
是,这current
是一个nullptr
。您想将其设置为othernode
.
您还需要添加另一个else
:
} else {
break;
这处理了current
不为空但othernode
为空的情况。在这种情况下,你就完成了。但是你的代码会一直循环。
更好的解决方案可能是while
将
while (current != this->head_ && othernode != other.head_) {
并摆脱内部if
。然后你可以检查
if (othernode != other.head_) {
while
循环之后。然后就做
pre->next->next = othernode;
那将摆脱break
.
这也摆脱了nullptr
检查和分配。
下面的代码也有问题。
current = current->next;
othernode = othernode->next;
opre = othernode->prev;
onex = other.head_->next->next;
pre = current->prev;
nex = current->next;
current->next = othernode;
othernode->prev = current;
nex->prev = othernode;
othernode->next = nex;
current = nex;
othernode = onex;
您不想要current = current->next
,因为它会推进指针并且您已经将其推进为head_->next
or 或nex
。所以只要摆脱那条线和othernode = othernode->next
.
onex = other.head_->next->next;
如前所述,这不应引用head_
. 这使得它在每次迭代中都具有相同的值。只是
onex = othernode->next;
我认为这就是一切,但我还没有尝试运行它。可能还有更多我没有注意到的错误。
推荐阅读
- python - 我编写了一个代码来在 python 中查看 ip 和 mac addr,但我不知道如何使文本出现在 gui 中
- java - 如何以正确的方式使用流过滤 HashMap?
- flutter - 在颤振中使用提供者类从用户对象接收空值
- coq - 使用可验证的 C 附加到链表时找出正确的循环不变量
- node.js - 使用gmail api返回相同结果的递归函数
- azure - 如何在 azure 中保护后端到后端 api 消耗
- lxc - 符合 OCI v1alpha1 的 CRIO 运行时规范
- php - Mysql中的Json字段在Twig中显示但在Controller中为空
- string - 如何在 Qt 字符串中单独截断主 URL
- python - Python Selenium Google 登录机器人