本程序实现了一个城市列表,采用单向链表来存储城市信息,提供了插入、删除、更新、按名字查找、按距离查找等功能。程序运行稳定,每个功能都经过了测试,可以在实际应用中使用。但需要注意的是,程序还有优化空间,例如可以添加错误处理机制和控制用户输入。本程序为初学者提供了一个简单实用的数据结构案例,可以借此理解并掌握链表的基本操作。
#include <iostream>
#include <cmath>
using namespace std;
struct CityNode {
string name;
double x;
double y;
CityNode* next;
};
class CityList {
private:
CityNode* head; // 头结点
public:
CityList() { // 构造函数
head = new CityNode();
head->name = "Header";
head->x = 0.0;
head->y = 0.0;
head->next = nullptr;
}
// 基本操作
void insert(string name, double x, double y); // 插入城市
bool remove(string name); // 删除城市
bool update(string name, double x, double y); // 更新城市信息
void print(); // 打印链表
double distance(double x1, double y1, double x2, double y2); // 计算两点距离
double searchByName(string name); // 按名字查找城市
void searchByDistance(double x, double y, double d); // 按距离查找城市
};
void CityList::insert(string name, double x, double y) {
CityNode* newNode = new CityNode();
newNode->name = name;
newNode->x = x;
newNode->y = y;
newNode->next = nullptr;
CityNode* p = head;
while (p->next != nullptr) {
p = p->next;
}
p->next = newNode;
}
bool CityList::remove(string name) {
CityNode* p = head;
while (p->next != nullptr) {
if (p->next->name == name) {
CityNode* temp = p->next;
p->next = temp->next;
delete temp;
return true;
}
p = p->next;
}
return false;
}
bool CityList::update(string name, double x, double y) {
CityNode* p = head->next;
while (p != nullptr) {
if (p->name == name) {
p->x = x;
p->y = y;
return true;
}
p = p->next;
}
return false;
}
void CityList::print() {
CityNode* p = head->next;
while (p != nullptr) {
cout << "城市名:" << p->name << ", X: " << p->x << ", Y: " << p->y << endl;
p = p->next;
}
}
double CityList::distance(double x1, double y1, double x2, double y2) {
double dx = x2 - x1;
double dy = y2 - y1;
return sqrt(dx * dx + dy * dy);
}
double CityList::searchByName(string name) {
CityNode* p = head->next;
while (p != nullptr) {
if (p->name == name) {
return p->x + p->y;
}
p = p->next;
}
return -1.0;
}
void CityList::searchByDistance(double x, double y, double d) {
CityNode* p = head->next;
while (p != nullptr) {
double dist = distance(x, y, p->x, p->y);
if (dist <= d) {
cout << "城市名: " << p->name << ", X: " << p->x << ", Y: " << p->y << endl;
}
p = p->next;
}
}
int main() {
CityList list;
list.insert("上海", 31.22, 121.48);
list.insert("北京", 10.10, 100.10);
list.insert("广州", 23.13, 113.26);
list.insert("深圳", 22.54, 114.06);
string cs;
while (true) {
cout << "-------------------------" << endl;
cout << "操作目录" << endl;
cout << "1. 插入一个城市" << endl;
cout << "2. 删除一个城市" << endl;
cout << "3. 修改城市信息" << endl;
cout << "4. 打印城市信息" << endl;
cout << "5. 搜索城市(城市名)" << endl;
cout << "6. 搜索城市(坐标)" << endl;
cout << "7. 退出" << endl;
cout << "-------------------------" << endl;
cout << "请输入你的选择: ";
int choice;
cin >> choice;
if (choice == 1) {
cout << "请输入你要添加的城市名称,X坐标以及Y坐标: ";
double x, y;
cin >> cs >> x >> y;
list.insert(cs, x, y);
list.print();
break;
}
if (choice == 2) {
cout << "请输入城市名称: ";
cin >> cs;
if (list.remove(cs)) {
cout << "删除成功" << endl;
list.print();
}
else {
cout << "城市未找到" << endl;
}
break;
}
if (choice == 3) {
double x, y;
cout << "请输入城市名称,新的X坐标和Y坐标: ";
cin >> cs >> x >> y;
if (list.update(cs, x, y)) {
cout << "更新成功" << endl;
list.print();
}
else {
cout << "城市未找到" << endl;
}
break;
}
if (choice == 4) {
list.print();
break;
}
if (choice == 5) {
cout << "请输入城市名称:";
cin >> cs;
double s = list.searchByName(cs);
if (s == -1.0) {
cout << "城市未找到" << endl;
}
else {
cout <<cs<< "坐标为" << s << endl;
}
break;
}
if (choice == 6) {
double x, y,s;
cout << "请输入城市坐标: ";
cin >> x >> y >> s;
list.searchByDistance(x, y, s);
break;
}
if (choice == 7)
{
cout << "谢谢使用" << endl;
return 0;
}
if (choice != 1 && choice != 2 && choice != 3 && choice != 4 && choice != 5 && choice != 6 && choice != 7)
{
cout << "无效选择" << endl;
break;
}
}
}
以下为效果截图