《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();
        }
    }
}

输出结果:

相关推荐
mudtools18 小时前
.NET驾驭Word之力:玩转文本与格式
c#·.net
唐青枫1 天前
C#.NET 数据库开发提速秘籍:SqlSugar 实战详解
c#·.net
mudtools2 天前
.NET驾驭Word之力:理解Word对象模型核心 (Application, Document, Range)
c#·.net
Fanxt_Ja2 天前
【LeetCode】算法详解#15 ---环形链表II
数据结构·算法·leetcode·链表
大飞pkz2 天前
【设计模式】C#反射实现抽象工厂模式
设计模式·c#·抽象工厂模式·c#反射·c#反射实现抽象工厂模式
今后1232 天前
【数据结构】二叉树的概念
数据结构·二叉树
唐青枫2 天前
从入门到进阶:C#.NET Stopwatch 计时与性能测量全攻略
c#·.net
未来之窗软件服务2 天前
幽冥大陆(二)RDIFSDK 接口文档:布草洗涤厂高效运营的技术桥梁C#—东方仙盟
开发语言·c#·rdif·仙盟创梦ide·东方仙盟
1uther2 天前
Unity核心概念⑨:Screen
开发语言·游戏·unity·c#·游戏引擎
阿幸软件杂货间2 天前
Office转PDF转换器v1.0.py
开发语言·pdf·c#