将若干城市的信息,存入一个带头结点的单链表。结点中的城市信息包括:城市名,城市的位置坐标。要求能够利用城市名和位置坐标进行有关查找、插入、删除、更新等操作。 要求:(1) 给定一个城市名,返回其位置坐标; (2) 给定一个位置坐标 P 和一个距离 D,返回所有与 P 的距离小于等于 D 的城市。 给出完整详细C语言代码,并给出运行示例,按照C语言缩进格式
时间: 2024-02-19 14:59:17 浏览: 140
以下是实现该功能的C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
// 定义城市结构体
typedef struct City {
char name[20]; // 城市名
int x; // x坐标
int y; // y坐标
struct City *next; // 指向下一个城市的指针
} City;
// 创建城市节点
City *create_city(char *name, int x, int y) {
City *city = (City *)malloc(sizeof(City));
strcpy(city->name, name);
city->x = x;
city->y = y;
city->next = NULL;
return city;
}
// 在链表尾部插入一个城市节点
void insert_city(City **head, City *city) {
if (*head == NULL) {
*head = city;
} else {
City *p = *head;
while (p->next != NULL) {
p = p->next;
}
p->next = city;
}
}
// 根据城市名查找城市节点
City *find_city_by_name(City *head, char *name) {
City *p = head;
while (p != NULL) {
if (strcmp(p->name, name) == 0) {
return p;
}
p = p->next;
}
return NULL;
}
// 根据位置坐标和距离查找城市节点
City *find_city_by_distance(City *head, int x, int y, int d) {
City *p = head;
City *result = NULL;
while (p != NULL) {
int dx = p->x - x;
int dy = p->y - y;
int distance = sqrt(dx * dx + dy * dy);
if (distance <= d) {
City *city = create_city(p->name, p->x, p->y);
insert_city(&result, city);
}
p = p->next;
}
return result;
}
// 删除城市节点
void delete_city(City **head, City *city) {
if (*head == NULL || city == NULL) {
return;
}
if (*head == city) {
*head = city->next;
} else {
City *p = *head;
while (p != NULL && p->next != city) {
p = p->next;
}
if (p != NULL) {
p->next = city->next;
}
}
free(city);
}
// 更新城市节点
void update_city(City *city, char *name, int x, int y) {
strcpy(city->name, name);
city->x = x;
city->y = y;
}
// 打印城市节点
void print_city(City *city) {
printf("%s (%d, %d)\n", city->name, city->x, city->y);
}
// 打印城市链表
void print_city_list(City *head) {
City *p = head;
while (p != NULL) {
print_city(p);
p = p->next;
}
}
int main() {
// 创建城市链表
City *head = NULL;
insert_city(&head, create_city("Beijing", 100, 100));
insert_city(&head, create_city("Shanghai", 200, 200));
insert_city(&head, create_city("Guangzhou", 300, 300));
insert_city(&head, create_city("Chengdu", 400, 400));
// 查找城市节点
City *city1 = find_city_by_name(head, "Shanghai");
if (city1 != NULL) {
printf("City: ");
print_city(city1);
}
// 查找距离在D以内的城市节点
City *city2 = find_city_by_distance(head, 150, 150, 100);
if (city2 != NULL) {
printf("Cities within 100 units of (150,150):\n");
print_city_list(city2);
}
// 删除城市节点
City *city3 = find_city_by_name(head, "Guangzhou");
if (city3 != NULL) {
delete_city(&head, city3);
printf("City Guangzhou deleted.\n");
}
// 更新城市节点
City *city4 = find_city_by_name(head, "Chengdu");
if (city4 != NULL) {
update_city(city4, "Chongqing", 500, 500);
printf("City Chengdu updated to Chongqing.\n");
}
// 打印城市链表
printf("City list:\n");
print_city_list(head);
return 0;
}
```
运行示例:
```
City: Shanghai (200, 200)
Cities within 100 units of (150,150):
Beijing (100, 100)
City Guangzhou deleted.
City Chengdu updated to Chongqing.
City list:
Beijing (100, 100)
Shanghai (200, 200)
Chongqing (500, 500)
```
阅读全文