数据结构—利用单链表操作城市信息

来源:互联网 发布:js隐式全局变量 编辑:程序博客网 时间:2024/06/05 03:53

效果截图:
这里写图片描述
这个用了一些简单的窗体应用和我自己编写的单链表来完成。
首先我创建了城市信息的类用以保存城市的信息

public class CityData{    public int X;    public int Y;    public string name;    public CityData(int x, int y, string name)     {        this.X = x;        this.Y = y;        this.name = name;    }}

然后由于需要查找城市,查找坐标等一系列操作,我写的单链表代码无法满足这些需求,于是我们创了一个单链表的子类

public class CityList : SLinkList<CityData> {     public CityData[] dataInfo = new CityData[10];     public double[] distances = new double[10];     private int CDLen = 0;     public CityData SearchPosInfo (string name)      {         SNode<CityData> 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<CityData> 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;       }   }}

最后就可以写我们的窗体代码了

private CityList list = new CityList();public Form1(){   InitializeComponent();}private void button1_Click(object sender, EventArgs e){   CityData data = this.makeData();   this.list.InsertAtFirst(data);   this.Update();}private CityData 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);   }   CityData city = new CityData(x,y,name);   return city;}private void Update(){   richTextBox1.Clear();   for (int i = 0; i < this.list.Length; i++)   {       CityData childData = list[i];       richTextBox1.AppendText(list[i].name + "\t" + list[i].X + "\t" + list[i].Y + "\n");   }}private void button2_Click(object sender, EventArgs e){   CityData data = this.makeData();   this.list.InsertAtEnd(data);   this.Update();}private void button3_Click(object sender, EventArgs e){   try   {       CityData data = this.makeData();       int index = Convert.ToInt32(numericUpDown1.Value);       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(numericUpDown2.Value);       this.list.Remove(index);   }   catch (Exception ex)    {       MessageBox.Show(ex.Message);   }   this.Update();}private void button5_Click(object sender, EventArgs e){   try    {       CityData data = this.makeData();       int index = Convert.ToInt32(numericUpDown1.Value);       list[index] = data;   }   catch (Exception ex)   {       MessageBox.Show(ex.Message);   }   this.Update();}private void button6_Click(object sender, EventArgs e){   string name = textBox4.Text;   CityData data = this.list.SearchPosInfo(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){   richTextBox2.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";                           }           richTextBox2.Text = str;       }   }   catch (Exception ex)   {       MessageBox.Show(ex.Message);   }}
0 0
原创粉丝点击