c++ - 如何在循环双向链表中使用指针

标签 c++ doubly-linked-list

我无法让编译器停止 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->nextpre->next->nextcurrent,它是一个 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下一个。因此,只需删除该行和 othernode = othernode->next

            onex = other.head_->next->next;

如前所述,这不应引用 head_。这使得它在每次迭代中都具有相同的值。只是

            onex = othernode->next;

我认为这就是一切,但我还没有尝试运行它。可能还有更多我没有注意到的错误。

关于c++ - 如何在循环双向链表中使用指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54375659/

相关文章:

c++ - 从 ffmpeg 读取输出的问题

c++ - 使用 STL 在 C++ 中实现 Bin 打包

c++ - 创建有序链表时缺少节点?

c++ - 链表搜索函数修改列表

c - 在链表末尾插入一个节点,.exe 崩溃

algorithm - 中间插入双链表

c++ - 什么是 g++-3/gcc-3?

c++ - 我怎样才能得到初始化程序的地址?

c++ - 第一次使用 Boost::asio::async_read 读取时出现 EOF

c - 为什么我的双向链表的元素没有正确显示?