《C#数据结构与算法》—201线性表

线性表的实现方式

顺序表

线性表的顺序存储是指在内存中用一块地址连续的空间依次存放线性表的数据元素,用这种方式存储的线性表叫顺序表。

特点:表中相邻的数据元素在内存中存储位置也相邻。

顺序表接口实现:

方法名 参数 返回值 描述
GetLength int 返回线性表的当前元素个数(长度)。
Clear void 清空线性表,重置内部元素计数为0,释放存储空间。
IsEmpty bool 判断线性表是否为空 (元素个数为0),返回true;否则返回false
Add T item void 在表尾追加元素。若容量不足,自动扩容。
Insert T item, int index void 在指定索引处插入元素 。索引需满足 0 ≤ index ≤ count,否则抛出异常。
Delete int index T 删除 并返回指定索引的元素。索引需满足 0 ≤ index < count,否则抛出异常。
GetEle int index T 获取指定索引的元素。索引越界时抛出异常。
索引器 int index T 通过索引访问元素 (如 list[0])。索引越界时抛出异常。
Locate T value int 返回第一个value相等的元素索引 ,未找到返回-1

代码: IList.cs

cs 复制代码
internal interface IList<T>
{
    int GetLength();//求长度
    void Clear(); //清空
    bool IsEmpty(); //判断是否为空
    void Add(T item);// 添加
    void Insert(T item ,int index);//插入
    T Delete(int index);//删除
    T GetEle(int index);//取表元
    T this[int index] { get; }//定义一个索引器, 获取元素
    int Locate(T value); //按值查找
}

顺序表的实现

代码的实现:SeqList.cs

1、定义字段:
cs 复制代码
private T[] data; //用来存储数据

private int count ;//表示存了多少个数据
2、实现构造函数 :
(1) 实现构造函数
cs 复制代码
 public SeqList(int size)  //size:数组的最大容量
 {
     data = new T[size];   // 初始化数组
     count = 0;            // 元素个数初始为0
 } 
(2) 默认构造函数(委托调用)
cs 复制代码
public SeqList():this(10) //默认构造函数 容量是10
{

}
3、添加基础方法
(1) 获取当前元素数量
cs 复制代码
public int GetLength()
{
    return count;
}
(2) 判断是否为空
cs 复制代码
public bool IsEmpty()
{
    return count == 0;
}
(3) 清空列表
cs 复制代码
public void Clear()
{
    count = 0;
}
4. 实现核心操作
(1) 添加元素(到末尾)
cs 复制代码
public void Add(T item)
{
    if(count == data.Length) //当前数组已经存满
    {
        Console.WriteLine("前顺序表已经存满,不允许再存入");
    }
    else
    {
        data[count] = item;
        count++;
    }
}
(2) 插入元素(到指定位置)
cs 复制代码
public T GetEle(int index)
{
    if(index>=0&& index<=count-1)
    {
        return data[index];
    }
    else
    {
        Console.WriteLine("索引不存在");
        return default(T);
    }
}
(3) 删除元素(按索引)
cs 复制代码
public T Delete(int index)
{
    T temp = data[index];

    for (int i = index + 1; i < count; i++)
    {
        data[i - 1] = data[i];
    }
    count--;
    return temp;
}
5. 实现访问与查找
(1) 按索引获取元素
cs 复制代码
public T GetEle(int index)
{
    if(index>=0&& index<=count-1)
    {
        return data[index];
    }
    else
    {
        Console.WriteLine("索引不存在");
        return default(T);
    }
}
(2) 索引器
cs 复制代码
public T this[int index]
{
    get { return GetEle(index); }
}
(3) 按值查找索引
cs 复制代码
public int Locate(T value)
{
    for (int i = 0; i < count; i++)
    {
        if (data[i].Equals(value))
        {
            return i;
        }
    }
    return -1;
}

代码使用:Program.cs

cs 复制代码
namespace _001_List
{
    internal class Program
    {
        static void Main(string[] args)
        {
            SeqList<string> seqList = new SeqList<string>();

            seqList.Add("123");
            seqList.Add("145");
            seqList.Add("167");

            Console.WriteLine("GetEle:"+seqList.GetEle(0));
            Console.WriteLine("this:"+seqList[0]);

            seqList.Insert("777", 1);
            for (int i = 0;i<seqList.GetLength(); i++)
            {
                Console.Write(seqList[i]+ " ");
            }
            Console.WriteLine("seqList:");

            seqList.Delete(0);
            for (int i = 0; i < seqList.GetLength(); i++)
            {
                Console.Write(seqList[i] + " ");
            }
            Console.WriteLine("seqList:");

            seqList.Clear();
            Console.WriteLine(seqList.GetLength()); 

            Console.ReadKey();
        }
    }
}

输出结果:

相关推荐
小猿_003 小时前
C语言指针进阶
c语言·数据结构·算法
丶Darling.9 小时前
Day126 | 灵神 | 二叉树 | 层数最深的叶子结点的和
数据结构·c++·算法·二叉树·深度优先
Kookoos9 小时前
ABP VNext + Orleans:Actor 模型下的分布式状态管理最佳实践
分布式·后端·c#·.net·.netcore·abp vnext
csdn_aspnet10 小时前
C# 高效读取大文件
c#
黎明smaly12 小时前
【C语言】复习~数组和指针
c语言·开发语言·数据结构·c++·leetcode
叒卮12 小时前
小白学习顺序表 之 通讯录实现
c语言·数据结构·学习
若汝棋茗12 小时前
C# 异步方法中缺少 `await` 运算符的隐患与解决方案
开发语言·c#·await
老农民编程12 小时前
C# 曲线编写总览
c#·wpf
点云SLAM12 小时前
PyTorch中cdist和sum函数使用详解
数据结构·人工智能·pytorch·python·点云数据处理·3d深度学习·张量计算
高远-临客13 小时前
unity控制相机围绕物体旋转移动
unity·c#·游戏引擎