关于链表的持续插入,求帮忙解决

List.cpp文件

List::List() {//初始化
head= NULL;
}
Node List::creatList() {//创建链表
head = (Node
)malloc(sizeof(struct Node));
Node*p1 = (Node*)malloc(sizeof(Node));
if (p1 == NULL) {
cout << "申请内存失败" << endl;
exit(0);
}
memset(p1, 0, sizeof(Node));
cout << "输入城市名称: ";
scanf_s("%s", p1->cityName, 20);
cout << "输入城市X坐标: ";
scanf_s("%d", &p1->x);
cout << "输入城市Y坐标: ";
scanf_s("%d", &p1->y);
head->Next = p1;
p1->Next = NULL;
cout << "creatList函数执行,链表创建成功" << endl;
return head;
}
void List::printList(Node*newList) {//遍历链表并打印到屏幕上
head = newList;
if (head == NULL) {
cout << "链表为空" << endl;
}
else {
head = head->Next;
while (head!= NULL) {
printf("%s (%d,%d)\n",head->cityName, head->x, head->y);
head = head->Next;
}
cout << endl;
}
}
Node*List::Insert(Node*pIn)//插入函数,每次都在头结点的下一个节点插入
{
pIn = (Node*)malloc((sizeof(Node)));
if (pIn == NULL) {
cout << "申请内存失败" << endl;
exit(0);
}
memset(pIn, 0, sizeof(Node));
cout << "输入城市名称: ";
scanf_s("%s", pIn->cityName, 20);
cout << "输入城市X坐标: ";
scanf_s("%d", &pIn->x);
cout << "输入城市Y坐标: ";
scanf_s("%d", &pIn->y);
pIn->Next = head->Next;
head->Next= pIn;
cout << "城市表头插入成功" << endl;
return head;
}

以下是main函数

#include"iostream"
#include"LinkList.h"
using namespace std;
void displayMenu();
static int choice;
void main() {
Node*pNew = (Node*)malloc(sizeof(Node));
Node*pInsert = (Node*)malloc(sizeof(Node));
List pList ;
while (true) {
displayMenu();
cout << "请选择:" << endl;
cin >> choice;
switch (choice) {
case 1:
cout << "输入新增城市信息" << endl;
pNew=pList.creatList();
break;
case 2:
pList.printList(pNew);
break;
case 3:
pNew=pList.Insert(pInsert);
break;
case 4:
cout << "case 4";
//Delete(&pList);
break;
case 5:
pList.find();
break;
default:
break;
}
}
}
void displayMenu() {
cout << " 【 【城市链表】 】 " << endl;
cout << "---------------------------------------------------------------------------" << endl;
cout << " 1.创建城市链表" << endl;
cout << " 2.更新显示所有城市" << endl;
cout << " 3.插入城市" << endl;
cout << " 4.删除城市" << endl;
cout << " 5.查找城市返回坐标" << endl;
}

是调用了printList函数之后再插入就有问题了

你的LinkList.h没贴,没法帮你调试。
你说是调用printList出问题的?
你printList函数遍历链表应该自己弄个临时变量,而不是拿head去做遍历
你用head的话,遍历完以后你的head就是NULL了,已经指向链表尾了
你也可以自己单步调试找找错误。
单步调试和设断点调试(VS IDE中编译连接通过以后,按F10或F11键单步执行,按Shift+F11退出当前函数;在某行按F9设断点后按F5执行停在该断点处。)是程序员必须掌握的技能之一。