C#顺序表 & 单向链表(无头)

C# 顺序表非常标准的顺序表结构,等同于C#中的List,但是List在排错查询和数据结构替换上存在缺陷,一些情况会考虑使用自己定义的数据结构1.优化方向 下表使用冒泡排序,可以考虑优化算法  C# 链表(单向无头)单向非闭环,无表头的链表,一般用于非队尾数据需要进行频繁删减的情况,由于没有表头所有排序算法写的有点low1.优化方向,改成双向链表,但是改成双向链表会花点时间因为指针的操作比较多,很容易出现闭环的情况如下图 测试运行程序用于测试上述表的运行程序 & 结果
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//包含命名空间 MyList
using MyList;
namespace DataStructure
{
    class Program
    {
        static void Main(string[] args)
        {
            //随机名字的数组
            string[] rNameSpace = new string[] {"蒋伟","吴悦","曾真","刘芸",
            "珠海妮","李小满","薛涵","邱伊雨","李晶晶","戴林江","代安东","黄挺",
                "陈政","叶小青","徐逸"};
            //随机用户数
            //(PS:测试用,没写限制 所以不要太大 最好不要超过10个)
            const int studentCount = 5;
            //开堆
            //var team0 = new MyArrayList<Student>(3);
            var team0 = new MyLinkList<Student>();
            Student[] students = new Student[studentCount];
            //随机出现数据 并添加 当随机种子一样是数据一样
            Random ra = new Random(6);
            for (int i = 0; i < studentCount;)
            {
                int id = ra.Next(1, 30);
                string name = rNameSpace[ra.Next(0, rNameSpace.Length)];
                bool isGirl = ra.Next(0, 2) == 0 ? false : true;
                Student s0 = new Student(id, name, isGirl);
                //查找是否有相同的属性
                if (team0.Contains(s0, StudentJudgeFunc0))
                {
                    continue;
                }
                else
                {
                    team0.Add(s0);
                    ++i;
                }
            }
            //遍历显示 & 属性调用
            Console.WriteLine(""); Console.WriteLine("==================== 分割线 ===================="); Console.WriteLine("");
            team0.Show(ShowFunc);
            Console.WriteLine();
            //Console.WriteLine(string.Format("当前顺序表 t--- t容量 :tt{0}", team0.Capacity));
            Console.WriteLine(string.Format("当前XX表 t--- t大小 :tt{0}", team0.Size));
            Console.WriteLine(string.Format("下表索引器测试 t--- t下标3姓名 :t{0}", team0[3].m_name));
            Console.WriteLine(""); Console.WriteLine("==================== 排序后 ===================="); Console.WriteLine("");
            team0.SortSmallToBig(StudentJudgeFunc0);
            team0.Show(ShowFunc);
            Console.WriteLine(""); Console.WriteLine("==================== 插入&删除测试 ===================="); Console.WriteLine("");
            team0.Insert(new Student(16, rNameSpace[1], true), 2);
            team0.Delete(1);
            team0.Show(ShowFunc);
            Console.WriteLine(""); Console.WriteLine("==================== 清空测试 ===================="); Console.WriteLine("");
            team0.Clear();
            team0.Show(ShowFunc);
        }
        //比较函数
        static int StudentJudgeFunc0(Student s0, Student s1)
        {
            if (s0.m_id == s1.m_id || s0.m_name.Equals(s1.m_name))
                return 0;
            if (s0.m_id > s1.m_id)
                return 1;
            else
                return -1;
        }
        //显示函数
        static void ShowFunc(Student _stu)
        {
            Console.WriteLine(string.Format("{1}t[{0}]t ---- t{2}", _stu.m_id, _stu.m_name, _stu.m_isGirl ? "Gril" : "Boy"));
        }
    }
    class Student
    {
        //学号
        public int m_id;
        //姓名
        public string m_name;
        //性别
        public bool m_isGirl;
        //构造
        public Student(int _id, string _name, bool _isGirl)
        {
            m_id = _id;
            m_name = _name;
            m_isGirl = _isGirl;
        }
    }
}

相关内容推荐