数据结构——链表的习题

拟墨画扇 提交于 2020-03-08 13:27:33

题目

将若干城市的信息,存入一个单链表。结点中的城市信息包括:城市名,城市的位置坐标。要求能够利用城市名和位置坐标进行有关查找、插入、删除、更新等操作。
[基本要求]
(1) 能在单链表中插入、删除、更新城市信息;
(2) 给定一个城市名,返回其位置坐标;
(3) 给定一个位置坐标P和一个距离D,返回所有与P的距离小于等于D的城市。

链表

链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成。每个结点包括两个部分:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域。

在这里插入图片描述

代码(C++)

借鉴网上的代码
C#没有链表的相关函数,需要自己编写,所以还是C++好用一些
插入函数写了简单的在最后插入,没有写在中间插入

#include <iostream>
#include <math.h>
#include<conio.h>
#include<string>
using namespace std;
struct Node //创建结点
{
	char Name[50];
	double x;
	double y;
	Node* next;
};
class List 
{
private:
	Node* head, * f;
	int size;
public:
	List();
	void Insert();
	void Delete();
	void Search();
	void distance();
	void show();
	void update();
};
List::List()  //构造
{
	head = new Node;
	f = head;
	size = 0;
}
void List::Insert()
{
	Node* p = new Node;
	cout << "请给出你要插入的城市的信息:";
	cin >> p->Name;
	cin >> p->x;
	cin >> p->y;
	if (size == 0)
	{
		cout << "城市地图上没有任何城市信息!\n";
		f->next = p;
		f = p;
		p->next = NULL;   //null表示最后一位
		size++;
		return;
	}
	else
	{
		f->next = p;
		size++;
		f = p;
		p->next = NULL;
	}
}
void List::Delete()
{
	Node* p;
	Node* t;
	char name[50];
	cout << "请给出你要删除的城市的名称:";
	cin >> name;
	for (p = head; p->next != NULL; p = p->next)
		if (!strcmp(p->next->Name, name))  
		//字符串比较函数 string compare 的缩写
		{
			t = p->next;
			p->next = t->next;
			delete(t);
			size--;
			break;
		}
}
void List::Search()
{
	char name[20];
	Node* p;
	cout << "请给出你想查询的城市的名称:";
	cin >> name;
	for (p = head->next; p != NULL; p = p->next)
		if (!strcmp(p->Name, name))
		{
			cout << p->Name;
			cout << '(' << p->x << ',' << p->y << ')' << endl;
			break;
		}
}
void List::update()
{
	char name[20];
	Node* p;
	cout << "请给出你想查询的城市的名称:";
	cin >> name;
	for (p = head->next; p != NULL; p = p->next)
		if (!strcmp(p->Name, name))
		{
			cout << "请输入新的城市名称:";
			cin >> p->Name;
			cout << "请输入城市的坐标x,y:";
			cin >> p->x >> p->y;
			break;
		}
}
void List::distance()
{
	char name[20];
	Node* p;
	Node n;
	int i;
	double d;
	double de;
	cout << "你给的城市是 :";
	cin >> name;
	cout << "你给的距离是 :";
	cin >> d;
	for (p = head->next; p != NULL; p = p->next)
		if (!strcmp(p->Name, name))
			break;
	n = *p;    //危险操作!?
	p = head->next;
	for (i = 0; i < size; i++, p = p->next)
	{
		de = sqrt(((*p).x - n.x) * ((*p).x - n.x) + ((*p).y - n.y) * ((*p).y - n.y));
		//C#中为System.Math.Sqrt()
		if (de <= d && strcmp(p->Name, name))
		{
			cout << p->Name;
			cout << '(' << p->x << ',' << p->y << ')' << endl;
		}
	}
}
void List::show()
{
	Node* p;
	p = head->next;
	int i;
	for (i = 0; i < size; i++)
	{
		cout << p->Name;
		cout << '(' << p->x << ',' << p->y << ')' << endl;
		p = p->next;
	}
}
void main()
{
	List t;
	int k;
	do {
		cout << "\n\n 1.增加城市";
		cout << "\n\n 2.删除城市";
		cout << "\n\n 3.修改城市信息";
		cout << "\n\n 4.根据城市名查询其坐标";
		cout << "\n\n 5.给坐标P和距离D,查询所有与P的距离小于等于D的城市";
		cout << "\n***************************************************************" << endl;
		cout << "\n 请输入你的选择操作(1、2、3、4、5) :\n";
		cin >> k;
		try
		{
		if (k>0&&k<6)
		{
		switch (k)
		{
		case 1: {
			t.Insert();
			t.show();
		}break;
		case 2: {
			t.Delete();
			t.show();
		}break;
		case 3: {
			t.update();
			t.show();
		}break;
		case 4: {t.Search(); }break;
		case 5: {t.distance(); }break;
		default: cout<<"别调皮";break;
		}
	} while (k > 0 && k < 6);
	cout << " 按任意键返回......";
	_getch();
	return;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!