创建和链接双链表

Creating and linkin doubly linked list

本文关键字:链表 链接 创建      更新时间:2023-10-16

你好,我一直试图调试这段代码很长一段时间,但不知道为什么。如有任何帮助,不胜感激。

在这里,我试图复制两个双链表(不同长度)到一个新的。然而,当我返回新创建的链表时,节点没有连接。谁能告诉我我哪里做错了?

struct PolyNode
{
    int coef;
    int expx;
    int expy;
    PolyNode* prev;
    PolyNode* next;
}
PolyNode* padd(PolyNode* a, PolyNode* b)
{
    PolyNode* c = new PolyNode;
    PolyNode* c_head =c;
    PolyNode* a_head =a;
    PolyNode* b_head =b;
    c->prev = nullptr;
    c->next = nullptr;
    PolyNode* c_next = c->next;
    PolyNode* c_prev = c->prev;
    while (a != nullptr)
    {
        c->coef = a->coef;
        c->expx = a->expx;
        c->expy = a->expy;
        cout << "tt copied c=" << c->coef << c->expx << c->expy << endl;
        if(a->next != nullptr)
        {
            c_next = new PolyNode;
            c_next->prev = c;
            c_prev = c;
            c = c_next;
            a = a->next;
        }
        else
        {
            c_next = new PolyNode;
            c_next->prev = c;
            c_prev = c;
            c = c_next;
            break;
        }
    }
    while (b != nullptr)
    {
        c->coef = b->coef;
        c->expx = b->expx;
        c->expy = b->expy;
        cout << "tt copied c=" << c->coef << c->expx << c->expy << endl;
        if(b->next != nullptr)
        {
            c_next = new PolyNode;
            c_next->prev = c;
            c = c_next;
            c_prev = c;
            b = b->next;
        }
        else
        {
            c_next = nullptr;
            break;
        }
    }
    c_next = nullptr;
    int sum = Polylength(a_head) + Polylength(b_head);
    for(int i =0; i< sum-1 ; i++)
    {
        if(c_prev == nullptr)
        {
            break;
        }
        c_next = c;
        c = c_prev;
        c_prev = c_prev->prev;
    }
    c_next = c;
    c = c_prev;
    c_prev = nullptr;
    sortPoly(c); //sortPoly is a function which sorts polynomials from largest to smallest exponents and sum them up if same exponents.
    return c;
}

我也不是一个很有经验的程序员,我正在努力培养一些好的编程习惯。请建议处理指针和链表的常见最佳实践。谢谢!但这里的问题是,即使在函数的开始,事情也会出错,我返回的节点没有任何到下一个节点的链接。

[EDIT stuyckp]正确缩进代码,并在最后一句添加问题语句

这段代码充满了bug,如果你正在学习使用指针,你真的必须一步一步地学习,并绘制出你使用指针所做的事情。我这样做是为了验证你的代码,问题很快就出现了。

你的基本错误是你似乎认为以下几点:

PolyNode* c_next = c->next;
PolyNode* c_prev = c->prev;

将以某种方式创建一个仍然属于c节点的变量,而实际上c->next的值只是null,并且该值被复制到一个新变量中。那么如果你改变了这个新变量,你根本没有改变c->。

我不打算解决你所有的问题,但这是朝着正确方向迈出的一步。不要使用所有这些临时变量,而是这样做:
while (a != nullptr)
{
    c->coef = a->coef;
    c->expx = a->expx;
    c->expy = a->expy;
    cout << "tt copied c=" << c->coef << c->expx << c->expy << endl;
    if(a->next != nullptr)
    {
        c->next = new PolyNode;//make c's next point to a new node
        c->next->prev = c; //make this new nodes prev point to c
        c->next->next = nullptr; //make sure to indicate the end
        c = c_next; //now move c to the next position
        a = a->next; //now move a to the next as well
    }
    ...

我仍然不喜欢这段代码,因为如果a和b都是空的,你仍然会分配一个新节点。我会在while循环中分配。并在一开始就将c初始化为null。

你的代码也做了很多事情,这将使调试和维护变得疯狂。这也是为什么有这么多复制粘贴代码的原因,while a循环和while b循环实际上是一样的。尝试创建一些细粒度的函数,并使用好名称来记录您在其中执行的操作。例如,对于这个大型函数来说,Padd是一个非常糟糕的名字。它不是添加,而是复制两个列表并从它们中创建一个新列表。

这也不是c++,它只是普通的c。在c++中,您将使用不同的方法,将行为添加到from of方法的数据结构中。我就不细讲了,我想你们有足够的内容可以继续。