c++中想用构建个线性表在用线性表中的数据求平均值,运行是报错,求解

img

程序执行到fun()函数后就执行不下去了。应该是执行了一次循环后就不行了。

img

试试这样(未测试)



int main()
{
    Node *arr1, *arr2;
    arr1 = arr2 = NULL;
    while (true) {
        int temp;
        cin >> temp;
        if (temp == 0) {
            break;
        }
        arr1 = new Node;
        arr1->data = temp;
        arr1->next = NULL;
    }
    cout << func(arr2) << endl;
    return 0;
}