using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ClassLibrary1;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
private CityList list = new CityList();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
City data = this.makeData();
this.list.InsertAtFirst(data);
this.Update();
}
private City makeData()
{
string name = textBox1.Text;
int x = 0;
int y = 0;
try
{
x = Convert.ToInt32(textBox2.Text);
y = Convert.ToInt32(textBox3.Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
City city = new City(x, y, name);
return city;
}
private void Update()
{
listBox1.Items.Clear();
for (int i = 0; i < this.list.length; i++)
{
City childData = list[i];
listBox1.Items.Add(list[i].name + "\t" + list[i].X + "\t" + list[i].Y + "\n");
}
}
private void button2_Click(object sender, EventArgs e)
{
City data = this.makeData();
this.list.InsertAtRear(data);
this.Update();
}
public void button3_Click(object sender, EventArgs e)
{
try
{
City data = this.makeData();
int index = Convert.ToInt32(textBox4.Text);
this.list.Insert(index, data);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
this.Update();
}
private void button4_Click(object sender, EventArgs e)
{
try
{
int index = Convert.ToInt32(textBox4.Text);
this.list.Remove(index);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
this.Update();
}
private void button5_Click(object sender, EventArgs e)
{
try
{
City data = this.makeData();
int index = Convert.ToInt32(textBox4.Text);
list[index] = data;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
this.Update();
}
private void button6_Click(object sender, EventArgs e)
{
string name = textBox4.Text;
City data = this.list.Search(name);
if (data == null)
{
MessageBox.Show("并没有该城市");
}
else
{
textBox5.Text = data.X.ToString();
textBox6.Text = data.Y.ToString();
}
}
private void button7_Click(object sender, EventArgs e)
{
listBox2.Items.Clear();
try
{
int pointX = Convert.ToInt32(textBox7.Text);
int pointY = Convert.ToInt32(textBox8.Text);
int distance = Convert.ToInt32(textBox9.Text);
this.list.SearchCity(distance, pointX, pointY);
int len = this.list.dataInfo.Length;
string str = "";
for (int i = 0; i < len; i++)
{
if (list.dataInfo[i] != null) {
str += list.distances[i].ToString("0.00") + "\t" +list.dataInfo[i].name + "\t" + list.dataInfo[i].X + "\t" + list.dataInfo[i].Y + "\n";
}
listBox2.Text = str;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
城市列表,实现查找城市
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CityList
{
public class CityList2 : SLinkList<City>
{
public City[] dataInfo = new City[100];
public double[] distances = new double[100];
private int CDLen = 0;
public City Search(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;
}
return null;
}
public void SearchCity(int distance, int pointX, int pointY)
{
SNode<City> temp = this.pHead;
for (int i = 0; i < this.length; i++)
{
double dis = Math.Sqrt(Math.Pow((temp.Data.X - pointX), 2) + Math.Pow((temp.Data.Y - pointY), 2));
if (dis <= distance)
{
dataInfo[this.CDLen++] = temp.Data;
distances[this.CDLen - 1] = dis;
}
temp = temp.Next;
}
}
public SNode<City> pHead { get; set; }
}
}
对单链的处理,删除、头插、尾插、插入、更新。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CityList
{
public class SLinkList<T>:ILinearList<T>where T:IComparable<T>
{
public SNode<T> phead;
public 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 void SLinklist()
{
phead = null ;
length = 0;
}
public SNode<T> Locate(int index)
{
if(index<0||index>length-1)
throw new ArgumentOutOfRangeException();
SNode<T>temp = phead ;
for (int i=0;i<index;i++)
{
temp = temp.Next;
}
return temp;
}
public void InsertAtFirst(T data)
{
if(length==0)
phead =new SNode<T>(data);
else
phead =new SNode<T>(data,phead);
length++;
}
public void InsertAtRear(T data)
{
if(length==0)
phead =new SNode<T>(data);
else
Locate(length -1).Next =new SNode<T>(data);
length ++;
}
public void Clear()
{
phead = null;
length = 0;
}
public void Insert(int index,T data)
{
if(index<0||index>length)
throw new ArgumentOutOfRangeException();
if(index==0)
InsertAtFirst(data);
else if(index ==length)
InsertAtRear(data);
else
{
SNode<T>temp =Locate(index-1);
temp.Next = new SNode<T> (data,temp.Next);
length ++;
}
}
public void Remove(int index)
{
if (index < 0 || index > length - 1)
throw new ArgumentOutOfRangeException();
if (index == 0)
phead = phead.Next;
else
{
SNode<T> temp = Locate(index - 1);
temp.Next = temp.Next.Next;
length--;
}
}
}
}
SLinkList接口
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CityList
{
public interface ILinearList<T>
{
T this[int index] { get; set; }
void Insert(int index, T data);
void Remove(int index);
}
}
结点的模板,定义了结点类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CityList
{
public class SNode<T>where T:IComparable<T>
{
public T Data { get; set; }
public SNode<T> Next{get;set;}
public SNode(T data)
{
Data = data;
Next = null;
}
public SNode(T data,SNode<T> next)
{
Data = data;
Next = next;
}
}
}
对城市的实体化
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CityList
{
public class City:IComparable<City>
{
public int X;
public int Y;
public string name;
public City(int x, int y, string name)
{
this.X = x;
this.Y = y;
this.name = name;
}
public int compareto(City other)
{
return other.name == name ? 0 : -1;
}
}
}