c# 实现线性表(一)(顺序表(MyList)

**

c# 实现线性表(一)(顺序表(MyList))

**

你好! 本篇文章将用C#向你讲述一个完整的顺序表

发稿理由

网上有很多关于c/c++关于线性表的内容,但C#的却很少,因为C#有自己的list,里面已经封装好了,下面我将用我自己写的代码创建一个自己的Mylist

1.参考文章:https://blog.csdn.net/qq_40985921/article/details/90709669
2.参考文章:https://www.bilibili.com/video/BV1px411f7pe?p=33

首先定义线性表的接口,方便以后的单链表等

还可添加其他的功能
在这里插入图片描述

接下来创建Mylist的泛型类并继承接口IList

创建泛型数组array
创建count来记录当前的元素个数
在这里插入图片描述

分别创建有参和无参两个构造函数

用构造函数来初始化数组的长度
在这里插入图片描述

继续创建两个属性,方便调用

在这里插入图片描述

完成了这些就可开始方法的书写了

Add添加元素

在这里插入图片描述

Insert插入元素

在这里插入图片描述

获取元素

为了方便使用索引器来获取元素
在这里插入图片描述
在这里插入图片描述

RemoveAt移除元素

在这里插入图片描述

Remove移除元素

在这里插入图片描述

最后完整代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApp3
{
    class MyList<T>:Ilsit<T>
    {
        private T[] array;//创建一个泛型的数组来存储数据
        private int count = 0;//表示当前添加的元素个数


        //创建最大容量的属性
        public int capaity
        {
            //最大容量等于数组的长度
            get { return array.Length; }
        }
        public int Count
        {
            get { return this.count; }
        }

        public MyList(int maxSize)
        {
            if (maxSize > 0)
            {
                array = new T[maxSize];
            }
        }

        public MyList()
        {
            //当不传入参数时,默认长度为0
            array = new T[0];
        }

        public void Add(T item)
        {
            //当最大的容量为0时,给定默认长度
            if (capaity == 0)
            {
                array = new T[2];
            }
            //当前数组中的元素个数大于等于最大容量时,扩充
            else if (count >= capaity)
            {
                var newArray = new T[count * 2];
                //for循环的代码可使用Array类中的copy方法Array.Copy(array, newArray, count);
                for (int i = 0; i < array.Length; i++)
                {
                    newArray[i] = array[i];
                }
                array = newArray;
            }
            array[count] = item;
            count++;
        }

        public void Insert(T item, int index)
        {
            if (capaity == 0)
            {
                throw new Exception("list为空");
            }
            if (index < 0||index>capaity)
            {
                throw new Exception("index不在范围内");
            }
            //当最大的容量为0时,给定默认长度
            if (capaity == 0)
            {
                array = new T[2];
            }
            //当前数组中的元素个数大于等于最大容量时,扩充
            else if (count >= capaity)
            {
                var newArray = new T[count * 2];
                //for循环的代码可使用Array类中的copy方法Array.Copy(array, newArray, count);
                for (int i = 0; i < array.Length; i++)
                {
                    newArray[i] = array[i];
                }
                array = newArray;
            }
            //整体向后移动index和index后元素
            for (int i =count-1 ; i >=index; i--)
            {
                array[i + 1] = array[i];
            }
            array[index] = item;
            count++;
        }

        public T GetItem(int index)
        {
            if (capaity == 0)
            {
                throw new Exception("list为空");
            }
            if (index < 0 || index >= count)
            {
                throw new Exception("index超出范围");
            }
            return array[index];
        }
        public T this[int index]
        {
            get { return GetItem(index); }
        }

        public void RemoveAt(int index)
        {
            if (capaity == 0)
            {
                throw new Exception("list为空");
            }
            if (index < 0 || index > capaity)
            {
                throw new Exception("index不在范围内");
            }
            for (int i = index; i < count-1; i++)
            {
                array[i] = array[i + 1];
            }
            count--;
        }

        public void Remove(T item)
        {
            if (capaity == 0)
            {
                throw new Exception("list为空");
            }
            for (int i = 0; i < count; i++)
            {
                if (item.Equals(array[i]))
                {
                    for (int j = i; j < count-1; j++)
                    {
                        array[i] = array[i + 1];
                    }
                    count--;
                    break;
                }
            }
        }
    }
}

  • 6
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值