问题
解答
代码实现
学习过泛型和接口后,我们就有一定的思路去解决这种问题,首先要明确两个概念。
1.城市的名称,坐标属于信息,通过一个City
类实现。
public class City : IComparable<City>
{
public string _name;
public double _x;
public double _y;
public City(string name, double x, double y)
{
_name = name;
_x = x;
_y = y;
}
public override string ToString()
{
return string.Format("城市:{0},X坐标:{1},Y坐标:{2}", _name, _x, _y);
}
public int CompareTo(City other)//实现IComparable<City>
{
return other._x == _x ? 0 : -1;
}
}
2.城市数据表属于单链表,由于其还有额外的操作,如给定名称查询城市坐标,给定一个坐标和距离查询距离该坐标小于该距离的城市,于是通过CityList
继承单链表SLinkList
实现。
public class CityList:SLinkList<City>
{
public City[] dataInfo = new City[50];
public double[] distances = new double[50];
public int CDLen = 0;
public City SearchPosInfo (string name) //给定城市名字返回坐标
{
SNode<City> temp = this.pHead;
for (int i = 0; i < this.Length; i++)
{
if (temp.Data._name == name)
{
return Locate(i).Data;
}
temp = temp.Next;