线性表代码示例——C#

重学数据结构,写了线性表示例,代码如下:


class LinearList
    {
        private int length;
        private int MaxSize;
        private int[] element;

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

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

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

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

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

            MaxSize /= 2;
            element = new int[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 int 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(int value)
        {
            for (int i = 0; i < length; i++)
            {
                if (value == 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, int 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]);
            }
        }

        /// <summary>
        /// 将两个整数对调
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        public void Swap(ref int a, ref int b)
        {
            a = a + b;
            b = a - b;
            a = a - b;

            //int temp = a;
            //a = b;
            //b = temp;
        }

        static void Main(string[] args)
        {
            //create
            LinearList list = new LinearList();
            Console.WriteLine("线性表创建成功~");
            Console.WriteLine("线性表长度为:{0} ", list.Length());

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

            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.WriteLine("element数组长度:{0}",list.element.Length);
        }
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值