单链表应用

在这里插入图片描述在这里插入图片描述代码:
1.city部分

namespace city
{
    public class City:IComparable<City>
    {
        public string name;
        public int Xpos;
        public int Ypos;
        public City(string _name, int X, int Y)
        {
            name = _name;
            Xpos = X;
            Ypos = Y;
        }
        public int CompareTo(City other)
        {
            if (other.name == name || other.Xpos == Xpos && other.Ypos == Ypos)
                return 0;
            else
                return -1;
        }
        public override string ToString()
        {
            return string.Format("{0},( {1},{2} )",name,Xpos,Ypos);
        }
        
    }
}


2.泛型接口部分、

namespace city
{
    public interface ILinearList<T>where T:IComparable<T>//定义泛型接口
    {
        int Length { get; }
        T this[int index] { get;set; }
        bool IsEmpty();
        void Remove(int index);
        int Search(T data);
        void Clear();
        void Insert(int index,T data);
        void Update(int index, T data);
    }
}

3.单链表结点部分·

namespace city
{
    public class SNode<T> where T : IComparable<T>//定义结点的形式。数据+下一个节点的位置
    {
        public T Data{get;set;}
        public SNode<T> Next { get; set; }
        public SNode(T data, SNode<T> next)
        {
            Data = data;
            Next = next;
        }
    }
}

4.由子类实现泛型接口部分

namespace city
{
    public class SLinkList<T> : ILinearList<T> where T : IComparable<T>
    {
        public SNode<T> arrow;
        public int _length; 
        public int Length 
        {
            get { return _length; }
        }
        public SLinkList()
        {
            _length = 0;
            arrow = null;
        }
        private SNode<T> Locate(int index)//获取指定位置i上结点的内容(地址)
        {
            if (index < 0 || index > Length - 1)
                throw new IndexOutOfRangeException();
            SNode<T> temp = arrow;
            for (int i = 0; i < index; i++)
            {
                temp = temp.Next;
            }
            return temp;
        }
        public T this[int index] 
        {
            get
            {
               
                return Locate(index).Data;//进一步获取节点的数据部分。
            }
            set 
            {
                if (index < 0 || index > Length - 1)
                    throw new IndexOutOfRangeException();
                Locate(index).Data = value;
            }
        }
        public void Clear()
        {
            arrow = null;
            _length = 0;
        }
        public bool IsEmpty()
        {
            return (_length == 0);
        }
        public void Remove(int index)
        {
            if (index < 0 || index > Length - 1)
                throw new IndexOutOfRangeException();
            if (index == 0)
                arrow = arrow.Next;
            else
            {
                SNode<T> tempp=Locate(index-1);
                tempp.Next = tempp.Next.Next;
            }
            _length--;
        }
        public int Search(T data)
        {
            int i;
            SNode<T> temp = arrow;
            for (i = 0; i < Length - 1; i++)
            {
                if (temp.Data.CompareTo(data) == 0)
                    break;
                temp = temp.Next;
            }
            return i == Length ? -1 : i;
        }
        public void InsertFirst(T data)
        {
            arrow = new SNode<T>(data, arrow);
            _length++;
        }
        public void InsertRear(T data)
        {
            if (arrow == null)
                arrow = new SNode<T>(data, null);
            else 
            {
                Locate(Length - 1).Next = new SNode<T>(data, null);
            }
            _length++;
        }
        public void Insert(int index, T data)
        {
            //if (index < 0 || index > Length - 1)
                //throw new IndexOutOfRangeException();
            if (index == 0)
            {
                InsertFirst(data);
            }
            else if (index == Length - 1)
            {
                InsertRear(data);
            }
            else
            {
                SNode<T> temp = Locate(index-1);
                temp.Next = new SNode<T>(data, temp.Next);
                _length++;
            }
        }
        public void Update(int index, T data)
        {
            Locate(index).Data = data;
        }
    }
}

5.小窗体的实现

namespace city
{
    public partial class Form1 : Form
    {
        private readonly ILinearList<City> al = new SLinkList<City>();//不加private readonly为什莫是只读?
       
        public Form1()
        {
            InitializeComponent();
            initstate();//不加返回值类型
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void yylabel3_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }
        public void initstate()
        {
            al.Insert(0, new City("城市A", 500, 500));
            al.Insert(1, new City("城市B", 700, 300));
            al.Insert(2, new City("城市C", 600, 1200));
            al.Insert(3, new City("城市D", 100, 20));
            al.Insert(4, new City("城市E", 120, 400));
            al.Insert(5, new City("城市F", 1200, 190));
            al.Insert(6, new City("城市G", 900, 809));
            al.Insert(7, new City("城市H", 20, 934));
            for (int i = 0; i < al.Length; i++)
            {
                CitylistBox1.Items.Add(al[i]);//索引器的用法
            }
        }
        private void InsertAtFirst_Click(object sender, EventArgs e)
        {
            if (CityNametB1.Text == null || XXtB1.Text == null || yytB1.Text == null)
                MessageBox.Show("参数不足", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            else
            {
                al.Insert(0, new City(CityNametB1.Text, int.Parse(XXtB1.Text), int.Parse(yytB1.Text)));
                CitylistBox1.Items.Clear();
                Updateall();
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }
        private void Updateall()
        {
            for (int i = 0; i < al.Length; i++)
            {
                CitylistBox1.Items.Add(al[i]);//索引器的用法
            }
        }
        private void InsertAtRear_Click(object sender, EventArgs e)
        {
            if (CityNametB1.Text == null || XXtB1.Text == null || yytB1.Text == null)
                MessageBox.Show("参数不足", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            else
            {
                al.Insert(al.Length - 1, new City(CityNametB1.Text, int.Parse(XXtB1.Text), int.Parse(yytB1.Text)));
                CitylistBox1.Items.Clear();
                Updateall();
            }
        }

        private void Insert_Click(object sender, EventArgs e)
        {
            if (CityNametB1.Text == null || XXtB1.Text == null || yytB1.Text == null)
                MessageBox.Show("参数不足", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            else
            {
                al.Insert(InsertnUD.DecimalPlaces, new City(CityNametB1.Text, int.Parse(XXtB1.Text), int.Parse(yytB1.Text)));
                CitylistBox1.Items.Clear();
                Updateall();
            }
        }
        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        private void Delete_Click(object sender, EventArgs e)
        {
            al.Remove(RemovenUD.DecimalPlaces);
            CitylistBox1.Items.Clear();
            Updateall();
        }

        private void Update_Click(object sender, EventArgs e)
        {
            al.Update(UpdatenUD.DecimalPlaces, new City(CityNametB1.Text, int.Parse(XXtB1.Text), int.Parse(yytB1.Text)));
            CitylistBox1.Items.Clear();
            Updateall();
        }
        private void CitylistBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void Spbtn_Click(object sender, EventArgs e)
        {
            int s;
            s=al.Search(new City(CityNametB2.Text, 0, 0));
            Xlabel.Text = al[s].Xpos.ToString();//int转换成string类型。
            Ylabel.Text = al[s].Ypos.ToString();
        }
        private void label4_Click(object sender, EventArgs e)
        {
        }

        private void Snbtn_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            int x0 = int.Parse(xxtB3.Text);
            int y0 = int.Parse(yytB3.Text);
            double d = double.Parse(DistancetB.Text);
            for (int i = 0; i < al.Length; i++)
            {
                if ((al[i].Xpos - x0) * (al[i].Xpos - x0) + (al[i].Ypos - y0) * (al[i].Ypos - y0) <= d*d)
                    listBox1.Items.Add(al[i]);
            }
        }
    }
}

6.运行图
在这里插入图片描述
在这里插入图片描述

发布了30 篇原创文章 · 获赞 4 · 访问量 902

猜你喜欢

转载自blog.csdn.net/weixin_45776347/article/details/104759975