C++ 如何在c++;?

C++ 如何在c++;?,c++,C++,大家好抱歉我的问题很模糊这不是我的本意。不管怎样,我一直试图使用CysErrt来排序C++中的链表。不幸的是,我收到了一个访问冲突错误,我将尝试使用屏幕截图显示我的错误。这是我的密码。你能帮我找出哪里出了问题吗 这是我的密码: #include <iostream> #include <iomanip> #include <fstream> #include <stdlib.h> #include <string>

大家好抱歉我的问题很模糊这不是我的本意。不管怎样,我一直试图使用CysErrt来排序C++中的链表。不幸的是,我收到了一个访问冲突错误,我将尝试使用屏幕截图显示我的错误。这是我的密码。你能帮我找出哪里出了问题吗

这是我的密码:

#include <iostream>
#include <iomanip>    
#include <fstream>    
#include <stdlib.h>   
#include <string>

using namespace std;    

struct listNode
{   
    int  id, inv;
    listNode  *next;

    listNode(int tempId, int tempInv, listNode *nxt);
};

listNode::listNode(int tempId, int tempInv, listNode *nxt)
    : id(tempId), inv(tempInv), next(nxt)    
{       
    // all values initialized using initializer list
}   

void merge(listType &root, nodePtr &first, nodePtr &mid, nodePtr &last)
{    
    nodePtr index = root.first;   
    nodePtr index2 = mid->next;

    int num;

    nodePtr first2 = first;    
    nodePtr last2 = last;  
    nodePtr mid2 = mid;

    for (first2; first2->id <= last->id; first2 = first2->next)
    {   
        if (index->id > mid->id)
        {
            swap(first->id, index2->id);
            mid = mid->next;
            index2 = mid;

        }
        else if (index2->id > last->id)
        {
            swap(first->id, index->id);
            root.first = root.first->next;
            index = root.first->next;
        }
        else if (first->id < index->id)
        {
            swap(first->id, index->id);
            root.first = root.first->next;
            index = root.first->next;
        }
        else
        {
            swap(first->id, index2->id);
            mid = mid->next;
            index2 = mid;
        }
    }
}

void mergeSort(listType& root, nodePtr &first  , nodePtr &last )
{
    if (root.last->id - root.first->id == 0)
    {
    }
    else if (root.first->next == root.last)
    {
        if (root.last->id < root.first->id)
        {
            //int temp = root.first->id;
            swap(root.first->id, root.last->id);
            //swap();
        }
    }
    else
    {
        nodePtr first = root.first;
        int i = 0;
        for (root.first; root.first->id < root.last->id; root.first = root.first->next)
        {
            i++;
        }
        int mid = i / 2;
        nodePtr merger = first;
        nodePtr merger2;
        root.first = first;
        for (int r = 0; r < mid; r++)
        {
            root.first = root.first->next;
            merger = root.first;
        }

        mergeSort(root, root.first, merger);
        merger2 = root.first->next;
        mergeSort(root, merger2, root.last);
        merge(root, root.first, merger, root.last);
    }
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
结构列表节点
{   
int id,inv;
listNode*下一步;
listNode(int-tempId、int-tempInv、listNode*nxt);
};
listNode::listNode(int-tempId、int-tempInv、listNode*nxt)
:id(tempId)、inv(tempInv)、next(nxt)
{       
//使用初始值设定项列表初始化的所有值
}   
无效合并(列表类型和根、nodePtr和first、nodePtr和mid、nodePtr和last)
{    
nodePtr index=root.first;
nodePtr index2=mid->next;
int-num;
nodePtr first2=第一个;
nodePtr last2=最后一个;
nodePtr mid2=mid;
对于(first2;first2->id;first2=first2->next)
{   
如果(索引->id>中间->id)
{
交换(第一个->标识,第二个->标识);
mid=mid->next;
index2=中等;
}
else if(index2->id>last->id)
{
交换(第一个->标识,索引->标识);
root.first=root.first->next;
index=root.first->next;
}
else if(第一个->标识<索引->标识)
{
交换(第一个->标识,索引->标识);
root.first=root.first->next;
index=root.first->next;
}
其他的
{
交换(第一个->标识,第二个->标识);
mid=mid->next;
index2=中等;
}
}
}
void mergeSort(列表类型和根、nodePtr和first、nodePtr和last)
{
if(root.last->id-root.first->id==0)
{
}
else if(root.first->next==root.last)
{
if(root.last->idid)
{
//int temp=root.first->id;
交换(root.first->id,root.last->id);
//交换();
}
}
其他的
{
nodePtr first=root.first;
int i=0;
for(root.first;root.first->idid;root.first=root.first->next)
{
i++;
}
int mid=1/2;
nodePtr合并=第一;
nodePtr合并2;
root.first=first;
对于(int r=0;rnext;
合并=root.first;
}
合并排序(root,root.first,合并);
merger2=root.first->next;
合并排序(root、merger2、root.last);
合并(root,root.first,合并,root.last);
}
}

对不起,这是可怕的显示这是我第一次尝试发布这样的东西,我曾经尝试过做这件事,但从来没有工作得很好。对不起,答案在您的屏幕截图中。你在这里:

for (first2; first2->id <= last->id; first2 = first2->next)

一种解决方案是在访问之前检查指针的有效性

for ( ; first2 && first2->id <= last->id; first2 = first2->next)
for(;first2&&first2->id;first2=first2->next)

事实上,我认为如果要检查
first2
是否为
NULL
,那么根据
last->id
检查
first2
是多余的last似乎足够了,但我认为OP希望迭代最后一个节点,而不是跳过它。
first2 is null
for ( ; first2 && first2->id <= last->id; first2 = first2->next)