城市链表
解题步骤
首先创建有关城市信息的类,其次再创一个子类来实现对城市的查询·删除·插入等操作,最后创建一个窗体,利用窗体来实现用户操作,以此来得到效果图。
部分代码如下
public class T
{
public int X;
public int Y;
public string name;
public T(int x, int y, string name)
{
this.X = x;
this.Y = y;
this.name = name;
}
}
public class ILinearList
{
int Length { get; }
T this[int index] { get; set; }
void Insert(int index, T data);
void Remove(int index);
int Search(T data);
}
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); }
}
由于有很多不懂的地方,所以参考了别人的代码。