C++ 遍历链表并修改或插入节点C++;

C++ 遍历链表并修改或插入节点C++;,c++,pointers,struct,linked-list,nodes,C++,Pointers,Struct,Linked List,Nodes,我正在尝试编写一个函数,它将遍历一个链表,其中的节点表示多项式的项。每个节点都包括系数(一个双重命名的系数)、功率(一个大小命名的功率)和链接(一个节点接受器*下一个)字段。调用该函数时使用一个双变量值(表示节点应具有的系数)和一个大小变量i(表示其幂)。函数应该遍历链表,查找具有幂i的节点。如果列表已包含幂为i的节点,则应更改其系数以保留新值。如果之前没有幂为i的节点,则应将该项与系数值相加。列表应按电源排序(即电源为3的节点应为列表中的节点3) 以下是我迄今为止编写的代码,尽管它当前生成以下

我正在尝试编写一个函数,它将遍历一个链表,其中的节点表示多项式的项。每个节点都包括系数(一个双重命名的系数)、功率(一个大小命名的功率)和链接(一个节点接受器*下一个)字段。调用该函数时使用一个双变量值(表示节点应具有的系数)和一个大小变量i(表示其幂)。函数应该遍历链表,查找具有幂i的节点。如果列表已包含幂为i的节点,则应更改其系数以保留新值。如果之前没有幂为i的节点,则应将该项与系数值相加。列表应按电源排序(即电源为3的节点应为列表中的节点3)

以下是我迄今为止编写的代码,尽管它当前生成以下错误:

Project 3.exe中0x0130D2FA处未处理的异常:0xC0000005:访问冲突写入位置0x0000000C

我不知道为什么会产生错误,所以这是我的第一个问题。第二,我认为我的函数可能有一些逻辑错误,不能正确地修改和创建新节点

我已经被难倒在这几天,不能测试我的其他功能没有这一个在工作秩序,所以任何帮助将不胜感激!谢谢大家!

void Poly::setCoeff(double value, size_t i)
{
    if (0 <= i){
        Node* prev = new Node();
        Node* curr = new Node();
        Node* newNode = new Node();
        newNode->coeff = value;
        newNode->power = i;
        curr = myHead;      // Initialize curr to myHead;
        if (curr != nullptr)
        {
            while (curr->next != nullptr && curr->power != i)
            {
                prev = curr;
                curr = curr->next;
            }
            if (curr->power == i)
            {
                curr->coeff = value;
            }
            else if (curr->next == nullptr && i == curr->power + 1)
            {
                curr->next = new Node;  // Creates a node at the end of the list
                curr = curr->next; // Points to that node
                curr->next = nullptr; // Prevents it from going any further
                curr->power = i;
                curr->coeff = value;
            }
            else
            {

                prev->next = newNode;
                newNode->next = curr;
            }
        }
        else
        {
            curr->next = newNode;
            curr = curr->next;
        }
    }
    else
    {
        throw std::out_of_range("Index out of range");
    }
}
void Poly::setCoeff(双值,大小\u t i)
{
如果(0)系数=值;
newNode->power=i;
curr=myHead;//将curr初始化为myHead;
if(curr!=nullptr)
{
while(curr->next!=nullptr&&curr->power!=i)
{
上一次=当前;
当前=当前->下一步;
}
如果(电流->功率==i)
{
电流->系数=值;
}
else if(curr->next==nullptr&&i==curr->power+1)
{
curr->next=new Node;//在列表末尾创建一个节点
curr=curr->next;//指向该节点
curr->next=nullptr;//阻止它继续
电流->功率=i;
电流->系数=值;
}
其他的
{
prev->next=newNode;
新建节点->下一步=当前;
}
}
其他的
{
curr->next=newNode;
当前=当前->下一步;
}
}
其他的
{
抛出标准::超出范围(“索引超出范围”);
}
}

<代码> > P>这是C++中如何管理动态内存的一系列错误的假设,这将使你陷入代码中的堆麻烦中。这不是一个学术性的练习吗?我只想把它扔到<强>所有< /强>,然后使用:

std::map<size_t, double>
这是否嵌套在
类Poly
中(如果是后者,则很可能应该嵌套在类Poly中)同样不清楚。这与问题并不完全相关,但这里提到的目的是试图让大家明白,在提出问题时,提供足够的信息,以尽量减少可能影响答案的假设

使用您的代码:

void Poly::setCoeff(double value, size_t i)
{
    if (0 <= i)  // NOTE: not needed, unsigned, will always be i >= 0
    {
        Node* prev = new Node();     // NOTE: wrong. leaks memory.
        Node* curr = new Node();     // NOTE: same as above
        Node* newNode = new Node();  // NOTE: **may** leak (see below)
        newNode->coeff = value;
        newNode->power = i;
        curr = myHead;
        if (curr != nullptr) // OK: check for null good
        {
            // NOTE: should be checking `curr`, not curr->next
            while (curr->next != nullptr && curr->power != i)
            {
                prev = curr;
                curr = curr->next;
            }

            // NOTE: should check curr for NULL first.
            if (curr->power == i)
            {
                curr->coeff = value;
            }

            // NOTE: same here. also, 
            else if (curr->next == nullptr && i == curr->power + 1)
            {
                // NOTE: this code path will leak newNode allocated at the
                //  top of the function.
                curr->next = new Node;
                curr = curr->next;
                curr->next = nullptr;
                curr->power = i;
                curr->coeff = value;
            }
            else
            {
                prev->next = newNode;
                newNode->next = curr;
            }
        }
        else
        {   // NOTE: this is where your mainline fault is coming from. you
            //  just validated curr can be NULL here (and will be on initial)
            curr->next = newNode;
            curr = curr->next;
        }
    }

    // NOTE: this can't happen, as i can never be less than zero
    else
    {
        throw std::out_of_range("Index out of range");
    }
}
有了这些,事情将变得相当容易。通过以下更改,可以完成上面的剩余工作清单:

void Poly::setCoeff(double value, size_t i)
{
    Node *prev = nullptr; // points to prior node
    Node *curr = myHead;  // points to current node

    while (curr && curr->power < i)
    {
        prev = curr;         // remember current node...
        curr = curr->next;   // ... then move to next node
    }

    // only allocate a new node if
    //  (a) we reached the end of the list (curr == NULL)
    //  (b) we reached a node with non match (will be larger exponent)

    if (!curr || curr->power != i)
    {
        // **NOW** allocate the new node. we know we need one and we
        //  have a pretty solid idea where it goes.
        Node *newNode = new Node(value, i, curr);

        // if prev is set, then it means the new node goes somewhere
        //  *past* the head pointer otherwise it will become the new head.
        if (prev)
            prev->next = newNode;
        else
            myHead = newNode;
    }
    else
    {   // found matching node
        curr->coeff = value;
    }
}
void Poly::setCoeff(双值,大小\u t i)
{
Node*prev=nullptr;//指向上一个节点
Node*curr=myHead;//指向当前节点
而(电流和电流->功率next;//…然后移动到下一个节点
}
//仅在以下情况下分配新节点
//(a)我们到达了列表的末尾(curr==NULL)
//(b)我们到达了不匹配的节点(将是更大的指数)
如果(!curr | curr->power!=i)
{
//**现在**分配新节点。我们知道我们需要一个节点,并且
//我有一个很好的想法。
Node*newNode=新节点(值,i,curr);
//如果设置了prev,则表示新节点在某个地方
//*超过*头部指针,否则将成为新头部。
如果(上一个)
prev->next=newNode;
其他的
myHead=newNode;
}
其他的
{//找到了匹配的节点
电流->系数=值;
}
}

<>我真诚地希望它能有所帮助,并祝你好运,在你到达好的东西之前,在漏洞中挖沟。最后,它值得。

< P>这是一系列错误的假设,它是如何在C++中管理动态内存,这会使你陷入这个代码中的堆麻烦中。这不是我要做的一个学术性的练习吗?l您只需将其全部扔掉并使用:

std::map<size_t, double>
这是否嵌套在
类Poly
中(如果是后者,则很可能应该嵌套在类Poly中)同样不清楚。这与问题并不完全相关,但这里提到的目的是试图让大家明白,在提出问题时,提供足够的信息,以尽量减少可能影响答案的假设

使用您的代码:

void Poly::setCoeff(double value, size_t i)
{
    if (0 <= i)  // NOTE: not needed, unsigned, will always be i >= 0
    {
        Node* prev = new Node();     // NOTE: wrong. leaks memory.
        Node* curr = new Node();     // NOTE: same as above
        Node* newNode = new Node();  // NOTE: **may** leak (see below)
        newNode->coeff = value;
        newNode->power = i;
        curr = myHead;
        if (curr != nullptr) // OK: check for null good
        {
            // NOTE: should be checking `curr`, not curr->next
            while (curr->next != nullptr && curr->power != i)
            {
                prev = curr;
                curr = curr->next;
            }

            // NOTE: should check curr for NULL first.
            if (curr->power == i)
            {
                curr->coeff = value;
            }

            // NOTE: same here. also, 
            else if (curr->next == nullptr && i == curr->power + 1)
            {
                // NOTE: this code path will leak newNode allocated at the
                //  top of the function.
                curr->next = new Node;
                curr = curr->next;
                curr->next = nullptr;
                curr->power = i;
                curr->coeff = value;
            }
            else
            {
                prev->next = newNode;
                newNode->next = curr;
            }
        }
        else
        {   // NOTE: this is where your mainline fault is coming from. you
            //  just validated curr can be NULL here (and will be on initial)
            curr->next = newNode;
            curr = curr->next;
        }
    }

    // NOTE: this can't happen, as i can never be less than zero
    else
    {
        throw std::out_of_range("Index out of range");
    }
}
有了这些,事情将变得相当容易。通过以下更改,可以完成上面的剩余工作清单:

void Poly::setCoeff(double value, size_t i)
{
    Node *prev = nullptr; // points to prior node
    Node *curr = myHead;  // points to current node

    while (curr && curr->power < i)
    {
        prev = curr;         // remember current node...
        curr = curr->next;   // ... then move to next node
    }

    // only allocate a new node if
    //  (a) we reached the end of the list (curr == NULL)
    //  (b) we reached a node with non match (will be larger exponent)

    if (!curr || curr->power != i)
    {
        // **NOW** allocate the new node. we know we need one and we
        //  have a pretty solid idea where it goes.
        Node *newNode = new Node(value, i, curr);

        // if prev is set, then it means the new node goes somewhere
        //  *past* the head pointer otherwise it will become the new head.
        if (prev)
            prev->next = newNode;
        else
            myHead = newNode;
    }
    else
    {   // found matching node
        curr->coeff = value;
    }
}
void Poly::setCoeff(双值,大小\u t i)
{
Node*prev=nullptr;//指向上一个节点
Node*curr=myHead;//指向当前节点
而(电流和电流->功率next;//…然后移动到下一个节点
}
//仅在以下情况下分配新节点
//(a)我们到达了列表的末尾(curr==NULL)
//(b)我们到达了不匹配的节点(将是更大的指数)
如果(!curr | curr->power!=i)
{
//**现在**分配新节点。我们知道我们需要一个节点,并且
//我有一个很好的想法。
Node*newNode=新节点(值,i
9^0
3^1
3^2
6^3