Visual c++ 未处理的异常访问冲突错误

Visual c++ 未处理的异常访问冲突错误,visual-c++,Visual C++,在这几行中,我一直面临着访问冲突错误。我仍然找不到它的根本原因。如果有人能帮我度过难关,我将不胜感激。谢谢 #include <iostream> using namespace std; struct Node { int data; Node *Next; }; struct list { int count; Node*Head; }; void IntializeList(list &L) { L.count=0; L.Head=NULL; }

在这几行中,我一直面临着访问冲突错误。我仍然找不到它的根本原因。如果有人能帮我度过难关,我将不胜感激。谢谢

#include <iostream>
using namespace std;

struct Node
{
int data;
Node *Next;
};

struct list
{
int count;
Node*Head;
};

void IntializeList(list &L)
{
    L.count=0;
    L.Head=NULL;
}

void AddElement(list &L,int DataIn)
{
    Node*Temp= new Node;
    Node*last=L.Head;
    while(last->Next!=NULL)
    {
        last=last->Next;
    }
    last->Next=Temp;
    Temp->data=DataIn;
    Temp->Next=NULL;
}

void count(Node*L)
{
    int c=0;
    while (L->Next!=NULL)
    {
        L=L->Next;
        c++;
    }

}

void DeleteList(Node*L)
{
    while(L->Next!=NULL)
    {
    Node*temp=L;
    L=L->Next;
    delete temp;
    }
}

void SplitList(list &L)
{
    Node*Mid=L.Head;
    Node*Final=L.Head;
    Node*L2=L.Head;
    /*Final=L->Head->Next->Next;
    Mid=L->Head->Next; */
    while(Final!=NULL)
    {
        Final=Final->Next->Next;
        Mid=Mid->Next;

    }

    Node*L3=Mid->Next;
    Mid->Next=NULL;
    cout<<"L2= ";
    while(L2!=NULL)
    {
        cout<<L2->data;
        L2=L2->Next;
    }

    cout<<"L3= ";
    while(L3!=NULL)
    {
        cout<<L3->data;
        L3=L3->Next;
    }
}

void main()
{
    list L1;
    IntializeList(L1);
    AddElement(L1,2);
    AddElement(L1,3);
    AddElement(L1,4);
    AddElement(L1,5);
    AddElement(L1,9);
    Node*Temp=L1.Head;
    //printing function
    while(Temp->Next!=NULL)
    {
        cout<<Temp->data;
        Temp=Temp->Next;
    }
    SplitList(L1);
}

首先,在向SO发布问题时,最好澄清您的问题。 由于您运行VisualC++,您可以尝试在调试模式下运行程序。它可能会向您显示错误发生或检测到的位置


因为头在第一次创建列表时被初始化为NULL,所以当您调用AddItem时,它将在空列表上失败,因为last->Next未定义last仍然为NULL。您需要检查last本身是否为NULL,而不是只检查下一项。您可能还需要对其他函数(如deleteList)执行相同的操作。

您需要提供一些回溯