void find(Node*pHead) {
char name[20];
cout << "输入你要查找城市的名称:" << endl;
scanf_s("%s", name, 20);
if (pHead == NULL) {
cout << "表为空" << endl;
}
while (!strcmp(pHead->cityName, name)&&pHead->Next!=NULL) {
pHead = pHead->Next;
}
if (!strcmp(pHead->cityName, name) && pHead != NULL) {
cout << "没有该城市" << endl;
}
if (strcmp(pHead->cityName, name)) {
cout << "找到";
cout << pHead->location_x << " " << pHead->location_y << endl;
}
}
//其中location_x和location_y是结构体
typedef struct Node {
char cityName[20];
int location_x, location_y;
struct Node *Next;
}Node;
你在哪里给location_x、location_y赋值的?
Node creatList(Node*pHead) {
Node*p1;
pHead = (Node)malloc(sizeof(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->location_x);
cout << "输入城市Y坐标: ";
scanf_s("%d", &p1->location_y);
pHead->Next = p1;
p1->Next = NULL;
cout << "creatList函数执行,链表创建成功" << endl;
return pHead;
}
//打印
void printList(Node*pHead) {
if (pHead == NULL) {
cout << "链表为空" << endl;
}
else {
pHead = pHead->Next;
while (pHead != NULL) {
printf("%s ", pHead->cityName,20);
printf("(%d,%d)\n", pHead->location_x, pHead->location_y);
pHead = pHead->Next;
}
cout << endl;
}
}
//插入城市
Node*Insert(Node**pNode) {
Node*pInsert;
Node*pHead,*temp;//temp为临时构造
pHead = pNode;
temp = pHead;
pInsert = (Node)malloc((sizeof(Node)));
memset(pInsert, 0, sizeof(Node));
cout << "输入城市名称: ";
scanf_s("%s", pInsert->cityName, 20);
cout << "输入城市X坐标: ";
scanf_s("%d", &pInsert->location_x);
cout << "输入城市Y坐标: ";
scanf_s("%d", &pInsert->location_y);
while (pHead->Next != NULL) {
pHead = pHead->Next;
}
pHead->Next = pInsert;
*pNode = temp;
cout << "城市表尾插入成功" << endl;
return pHead;
}