题目:![在这里插入图片描述](https://img-blog.csdnimg.cn/20200308214810356.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0taSDM0,size_16,color_FFFFFF,t_70)
- 运行效果:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200308214838202.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0taSDM0,size_16,color_FFFFFF,t_70)
1.思路:
- 先创建一个满足题目要求的窗体结构。
- 创建一个线性表接口,以及用链式存储结构实现这个线性表,使其实现插入,删除,更新等操作。
- 在主窗口完成封装。
- 由于 刚刚接触线性表,所以参考了别人的代码,敬请谅解。
2.代码(主要):
namespace DataStruct
{
public interface ILinearList<T> where T : IComparable<T>
{
int Length { get; }
T this[int index] { get; set; }
void Insert(int index, T data);
void Remove(int index);
int Search(T data);
}
}
public class SLinkList<T> : ILinearList<T> where T : IComparable<T>
{
private SNode<T> _pHead;
private int _length;
public T this[int index]
{
get
{
if (index < 0 || index > _length - 1)
throw new ArgumentOutOfRangeException();
return Locate(index).Data;
}
set
{
if (index < 0 || index > _length - 1)
throw new ArgumentOutOfRangeException();
Locate(index).Data = value;
}
}
public int Length
{
get
{
return _length;
}
}
public SLinkList()
{
_pHead = null;
_length = 0;
}
public void InsertAtFirst(T data)
{
if (_length == 0)
{
_pHead = new SNode<T>(data, null);
}
else
{
_pHead = new SNode<T>(data, _pHead);
}
_ length++;
}
}
Button btn = sender as Button;
string cityname = cityName.Text.Trim();
string cityx = cityX.Text.Trim();
string cityy = cityY.Text.Trim();
switch (btn.Name)
{
case "first_insert":
City city_first = new City(city_name, int.Parse(cityx), int.Parse(cityy));
((SLinkList<City>)_list).InsertAtFirst(city_first);
listBox1.Items.Insert(0,city_first);
break;
case "last_insert":
City city_last = new City(city_name, int.Parse(cityx), int.Parse(cityy));
((SLinkList<City>)_list).InsertAtRear(city_last);
listBox1.Items.Add(city_last);
break;
case "insert":
City city = new City(city_name, int.Parse(city_x), int.Parse(city_y));
string index = this.insert_index.Text;
_list.Insert(int.Parse(index), city);
listBox1.Items.Insert(int.Parse(index), city);
break;
case "delete":
string index_delete = this.delete_index.Text;
_list.Remove(int.Parse(index_delete));
listBox1.Items.RemoveAt(int.Parse(index_delete));
break;
case "update":
City city_update = new City(city_name, int.Parse(city_x), int.Parse(city_y));
_list[int.Parse(this.update_index.Text)] = city_update;
listBox1.Items.RemoveAt(int.Parse(this.update_index.Text));
listBox1.Items.Insert(int.Parse(this.update_index.Text), city_update);
break;
case "search":
SNode<City> temp=((SLinkList<City>)_list).LocateHead();
int i;
for (i = 0; i < _list.Length; i++)
{
if (temp.Data.Cityname.CompareTo(search_name.Text.Trim()) == 0)
{
search_X.Text = temp.Data.X.ToString();
search_Y.Text = temp.Data.Y.ToString();
break;
}
else
{
temp = temp.Next;
}
}
break;
case "search_city":
int x = int.Parse(round_x.Text.Trim());
int y = int.Parse(round_y.Text.Trim());
double d = double.Parse(distance.Text.Trim());
SNode<City> temp1 = ((SLinkList<City>)_list).LocateHead();
int j;
for(j=0;j<_list.Length;j++)
{
int x1=temp1.Data.X;
int y1=temp1.Data.Y;
double dist=Math.Sqrt((x1-x)*(x1-x)+(y1-y)*(y1-y));
if(dist<d)
{
CityDistance city_distance = new CityDistance(temp1.Data.Cityname, temp1.Data.X, temp1.Data.Y,dist);
listBox2.Items.Add(city_distance);
}
temp1=temp1.Next;
}
break;
}
}