查找城市窗体

要求:

基本要求
在这里插入图片描述

实现过程:

先创建一个线性表接口,再用链式存储方式实现这个线性表,实现头插、尾插、插入、删除、更新等操作。将城市封装成一个类。在窗体里通过button实现要求的各种操作。由于两个listbox中输出的格式不同,可再封装一个City_distance的类。

线性表接口代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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);
    }
}
节点代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataStruct
{
    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;
using System.Threading.Tasks;

namespace DataStruct
{
    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);
            }

            //_pHead = new SNode<T>(data, _pHead);
            _length++;
        }

        private SNode<T> Locate(int index)
        {
            if (index < 0 || index > _length - 1)
                throw new ArgumentOutOfRangeException();

            SNode<T> temp = _pHead;
            int i = 0;
            for (; i < index; i++)
            {
                temp = temp.Next;
            }
            return temp;
        }

        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 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);

                //Locate(index - 1).Next = new SNode<T>(data, Locate(index));
                _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--;
        }

        public int Search(T data)
        {
            SNode<T> temp = _pHead;
            int i = 0;
            for (; i < _length; i++)
            {
                if (temp.Data.CompareTo(data) == 0)
                    break;
                temp = temp.Next;
            }
            return i == _length ? -1 : i;
        }

        public SNode<T> LocateHead()
        {
            SNode<T> temp = _pHead;
            return temp;
        }
    }
}
City类的代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataStruct
{
    public class City:IComparable<City>
    {
        private string _cityname;
        private int _cityX;
        private int _cityY;
        public City(string name,int X,int Y)
        {
            _cityname = name;
            _cityX = X;
            _cityY = Y;
         }
        public string Cityname
        {
            get  { return _cityname;  }
        }
        public int X
        {
            get { return _cityX; }
        }
        public int Y
        {
            get { return _cityY; }
        }
        public override string ToString()
        {
            string cityname =  _cityname;
            string x = _cityX.ToString();
            string y = _cityY.ToString();
            return _cityname + "      " + "(" + x + "," + y + ")";
        }

        public int CompareTo(City other)
        {
            return other._cityname == _cityname ? 0 : -1;
        }
    }
}
City_distance类代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataStruct
{
    public class CityDistance:IComparable<CityDistance>
    {
         private string _cityname;
        private int _x;
        private int _y;
        private double _distance;

        public string Cityname
        {
            get { return _cityname; }
        }

        public int X
        {
            get { return _x; }
        }

        public int Y
        {
            get { return _y; } 
        }

        public double Distance
        {
            get { return _distance; }
        }

        public CityDistance(string name, int x, int y,double distance)
        {
            _cityname = name;
            _x = x;
            _y = y;
            _distance = distance;
        }

        public override string ToString()
        {
            string cityname =  _cityname;
            string x = _x.ToString();
            string y = _y.ToString();
            string D = _distance.ToString();
            return "D=" + D + "   " + cityname + "   " + "(" + x + "," + y + ")";
        }

        public int CompareTo(CityDistance other)
        {
            return other.Distance == _distance ? 0 : -1;
        }
    }
    
}

主窗体代码如下:
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 DataStruct;

namespace 线性表实验1
{
    public partial class Form1 : Form
    {
        private readonly ILinearList<City> _list = new SLinkList<City>();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }
       
        private void button(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            string city_name = cityName.Text.Trim();
            string city_x = cityX.Text.Trim();
            string city_y = cityY.Text.Trim();
            switch (btn.Name)
            {
                case "first_insert":
                    City city_first = new City(city_name, int.Parse(city_x), int.Parse(city_y));
                    ((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(city_x), int.Parse(city_y));
                    ((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;
            }
        }               
    }
}

代码实现结果:

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值