线性表代码示例2---C#

之前写了个线性表示例,但由于只能对整型数进行操作,将之改为泛型:

 public class LinearList<Item>
    {
        private int length;     //线性表元素长度
        private int MaxSize;    //线性表最大容量
        private Item[] element; //存放元素的数组

        //构造方法
        public LinearList()
        {
            MaxSize = 1;
            element = new Item[MaxSize];
            length = 0;
        }

        public bool IsEmpty() { return length == 0; }
        public int Length() { return length; }

        /// <summary>
        /// 扩展线性表大小,扩充为原来的2倍
        /// </summary>
        void ExtendSize()
        {
            Item[] arrTemp = new Item[length];
            for (int i = 0; i < length; i++)
            {
                arrTemp[i] = element[i];
            }

            MaxSize *= 2;
            element = new Item[MaxSize];
            for (int i = 0; i < length; i++)
            {
                element[i] = arrTemp[i];
            }
        }

        /// <summary>
        /// 缩小线性表大小,缩小为原来的1/2
        /// </summary>
        void ReduceSize()
        {
            Item[] arrTemp = new Item[length];
            for (int i = 0; i < length; i++)
            {
                arrTemp[i] = element[i];
            }

            MaxSize /= 2;
            element = new Item[MaxSize];
            for (int i = 0; i < length; i++)
            {
                element[i] = arrTemp[i];
            }
        }

        /// <summary>
        /// 查找第k个元素,并将值返回给value
        /// </summary>
        /// <param name="index"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool Find(int k, ref Item value)
        {
            if (k < 1 || k > length)
            {
                return false;
            }
            value = element[k - 1];
            return true;
        }

        /// <summary>
        /// 验证第k个元素是否存在
        /// </summary>
        /// <param name="k"></param>
        /// <returns></returns>
        public bool Find(int k)
        {
            if (k < 1 || k > length)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 返回值为value的值所在位置,返回0则为未找到
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public int Search(Item value)
        {
            for (int i = 0; i < length; i++)
            {
                if (value.Equals( element[i]))
                {
                    return i + 1;
                }
            }
            return 0;
        }

        /// <summary>
        /// 删除第k个元素
        /// </summary>
        /// <param name="k"></param>
        public void Delete(int k)
        {
            if (Find(k))
            {
                //从第k+1个元素开始,到最后一个元素结束,所有元素向前移动1
                for (int i = k; i < length; i++)
                {
                    element[i - 1] = element[i];
                }
                length--;
            }

            //当线性表长度小于最大容量的1/4,将表的容量缩小为原来的1/2
            if (length <= (MaxSize / 4))
            {
                ReduceSize();
            }
        }

        /// <summary>
        /// 在第k个元素之后插入值为value的元素
        /// </summary>
        /// <param name="k"></param>
        /// <param name="value"></param>
        public void Insert(int k, Item value)
        {
            if (k >= 0 && k <= length)
            {
                //当线性表已满,将其最大容量扩充为原来的2倍
                if (MaxSize == length)
                {
                    ExtendSize();
                }

                //从最后一个元素开始,到第k+1个元素结束,所有元素向后挪动1
                for (int i = length - 1; i >= k; i++)
                {
                    element[i] = element[i - 1];
                }
                //在第k个元素之后插入新元素
                element[k] = value;
                length++;
            }
        }

        /// <summary>
        /// 反序排列线性表元素
        /// </summary>
        public void Reverse()
        {
            //在数组element本身空间内操作
            for (int i = 0; i < length / 2; i++)
            {
                Swap(ref element[i], ref element[length - i - 1]);
            }

        }

        /// <summary>
        /// 打印线性表元素
        /// </summary>
        public void Output()
        {
            Console.WriteLine("共 {0} 个元素:", length);
            for (int i = 0; i < length; i++)
            {
                Console.WriteLine("第 {0} 个元素: {1} ,", i + 1, element[i].ToString());
            }
        }

        /// <summary>
        /// 将两个对象对调
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        public void Swap(ref Item a, ref Item b)
        {
            Item temp;
            temp = a;
            a = b;
            b=temp;
        }

    }

通过一个People类进行测试:

public class People
    {
        private string name;            
        private string identityCard;

        public string IdentityCard
        {
            get { return identityCard; }
            set { identityCard = value; }
        }
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public People() { }

        public People(string name, string identity)
        {
            Name = name;
            IdentityCard = identity;
        }

        public override string ToString()
        {
            return "Name: " + Name + ", ID: " + IdentityCard;
        }

        public override bool Equals(object obj)
        {
            People people = obj as People;
            return this.IdentityCard == people.IdentityCard;
        }

        public override int GetHashCode()
        {
            return this.IdentityCard.GetHashCode();
        }
    }

class Program
    {
        static void Main()
        {
            //create
            LinearList<People> list = new LinearList<People>();
            Console.WriteLine("线性表创建成功~");
            Console.WriteLine("线性表长度为:{0} ", list.Length());

            //insert
            //list.Insert(0, 1);
            for (int i = 0; i < 21; i++)
            {
                list.Insert(i, new People("SB"+i.ToString()+"号",i.ToString()));
                Console.WriteLine("在第{0}个元素后插入一个人{1}", i,"SB" + i.ToString() + "号");
            }

            list.Output();
            Console.WriteLine("线性表长度为:{0} ", list.Length());

            //delete
            list.Delete(5);
            Console.WriteLine("删除第5个元素");
            list.Delete(10);
            Console.WriteLine("删除第10个元素");

            //reverse
            list.Reverse();

            list.Output();
            Console.WriteLine("线性表长度为:{0} ", list.Length());
            Console.ReadKey();
        }
    }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值