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

输出结果:

相关推荐
张人玉2 小时前
C# TCP 客户端开发笔记(TcpClient)
笔记·tcp/ip·c#
Univin2 小时前
C++(10.4)
开发语言·数据结构·c++
张人玉5 小时前
C# 通讯关键类的API
开发语言·c#
Jayden_Ruan9 小时前
C++十进制转二进制
数据结构·c++·算法
Haooog9 小时前
98.验证二叉搜索树(二叉树算法题)
java·数据结构·算法·leetcode·二叉树
lixinnnn.11 小时前
贪心:火烧赤壁
数据结构·c++·算法
前端 贾公子12 小时前
《Vuejs设计与实现》第 5 章(非原始值响应式方案)下 Set 和 Map 的响应式代理
数据结构·算法
快乐是一切12 小时前
PDF底层格式之水印解析与去除机制分析
前端·数据结构
MHJ_13 小时前
Multi-Metric Integration(多指标集成)
数据结构
William_cl13 小时前
C# MVC 修复DataTable时间排序以及中英文系统的时间筛选问题
开发语言·c#·mvc