typedef struct No {
string name;
struct No* next;
}Node, * pNode;
pNode list(void) {
string val;
pNode HEAD = (pNode)malloc(sizeof(Node));
if (HEAD == nullptr) {
cout << "分配内存失败!" << endl;
exit(-1);
}
pNode end = HEAD;
end->next = nullptr;
int i;
cout << "请输入人数" << endl;
cin >> i;
cout << "请输入姓名" << endl;
for (int j = 0; j < i; j++) {
cin >> val;
pNode p_end = (pNode)malloc(sizeof(Node));
if (p_end == nullptr) {
cout << "分配内存失败!" << endl;
exit(-1);
}
p_end->name = val;
end->next = p_end; //出错点:访问地址冲突
p_end->next = nullptr;
end = p_end;
}
return HEAD;
}AAnonymous
相关分类