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

输出结果:

相关推荐
All for pursuit.16 分钟前
【二分查找-2】34.在排序数组中查找元素的第一个和最后一个位置
数据结构·c++·算法·leetcode
bro_Java6661 小时前
链表进阶2:双向链表的构建
java·数据结构·链表
梦帮科技2 小时前
量子张量网络破局大模型:从矩阵乘积态 (MPS) 到张量列 (TT-SVD) 低秩收缩全推导
网络·数据结构·数据库·线性代数·矩阵·架构·模拟退火算法
LOVE️YOU3 小时前
Python 数据结构的本质:位置、对象引用、Hash 与可变性
数据结构·python·哈希算法
不会就选b3 小时前
算法日常・每日刷题--<动态规划>1
java·数据结构·算法
shehuiyuelaiyuehao3 小时前
算法54,链表 两数相加
数据结构·算法·链表
Logic1014 小时前
C语言/数据结构位运算题解:异或XOR找出弹子游戏机中数量为奇数的数字——只出现奇数次
c语言·数据结构·数组·位运算·时间复杂度·算法题·异或性质
阿狗童鞋6 小时前
数据结构与算法实战指南
数据结构·算法
垆边人似月.7 小时前
华为机考题:明明的随机数
数据结构·算法·华为
lusklusklusk7 小时前
Python 四大数据结构详解:元组、列表、集合、字典的特点与增删改查
数据结构·windows·python