public class XList<T> : IEnumerable, IEnumerator
{
#region List 简单实现 /// <summary>
/// 存储数据 数组
/// </summary>
private T[] _items; /// <summary>
/// 默认容量
/// </summary>
private const int DefaultLength = 4; /// <summary>
/// 插入最后一个元素索引(初始化时没有元素,所以为 -1)
/// </summary>
private int _lastIndex = -1; /// <summary>
/// 无参构造
/// </summary>
public XList()
{
_items = new T[DefaultLength];
} /// <summary>
/// 带初始容量的构造
/// </summary>
/// <param name="length"></param>
public XList(int length)
{
if (length <= 0)
{
throw new Exception("长度必须大于0");
}
_items = new T[length];
} /// <summary>
/// 索引器
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public T this[int index]
{
get
{
CheckIndex(index); // 验证索引越界
return _items[index];
}
set { Insert(index, value); }
} /// <summary>
/// 添加元素
/// </summary>
/// <param name="t"></param>
public void Add(T t)
{
Insert(_lastIndex + 1, t);
} /// <summary>
/// 插入元素
/// </summary>
/// <param name="index"></param>
/// <param name="t"></param>
public void Insert(int index, T t)
{
if (index < 0)
{
throw new Exception("索引不能小于0");
}
KeepCapacity();
if (index <= _lastIndex) // 插入位置已有元素
{
T[] temp = new T[Capacity];
for (int i = 0; i < index; i++)
{
temp[i] = _items[i];
}
temp[index] = t;
for (int i = index; i <= _lastIndex; i++)
{
temp[i + 1] = _items[i];
}
_items = temp;
          _lastIndex++;
}
else if (index == _lastIndex + 1) // 在末尾插入
{
_items[++_lastIndex] = t;
}
else
{
throw new Exception("索引越界");
}
} /// <summary>
/// 按 索引 移除元素
/// </summary>
/// <param name="index"></param>
public void Remove(int index)
{
CheckIndex(index);
if (index != _lastIndex) // 移除元素不是最后一个元素
{
T[] temp = _items;
for (int i = index; i < _lastIndex; i++)
{
temp[i] = _items[i + 1];
}
_items = temp;
}
// 移除元素是最后一个元素,不做处理
_lastIndex--;
} /// <summary>
/// 集合存储元素数量
/// </summary>
/// <returns></returns>
public int Count()
{
return _lastIndex + 1;
} /// <summary>
/// 清空集合
/// </summary>
public void Clear()
{
_lastIndex = -1;
} /// <summary>
/// 集合 容量
/// </summary>
private int Capacity
{
get
{
if (_items != null)
{
return _items.Length;
}
return -1;
}
} /// <summary>
/// 保证容量充足,如果数组容量不够就重置大小,容量扩大一倍
/// </summary>
private void KeepCapacity()
{
if (Capacity == -1) // 数组为 null 的情况
{
_items = new T[DefaultLength];
return;
}
if (_lastIndex < Capacity - 1) // 容量足够
{
return;
}
// 容量不够
int length = Capacity * 2;
T[] temp = new T[length];
for (int i = 0; i < this.Capacity; i++)
{
temp[i] = _items[i];
}
_items = temp;
} /// <summary>
/// 检查索引越界的情况
/// </summary>
/// <param name="index"></param>
private void CheckIndex(int index)
{
if (index > _lastIndex)
{
throw new Exception("索引越界");
}
} #endregion #region 实现枚举 #region IEnumerable 成员 public IEnumerator GetEnumerator()
{
return this;
} #endregion #region IEnumerator 成员 private int _position = -1; public object Current
{
get { return _items[_position]; }
} public bool MoveNext()
{
if (_position < _lastIndex)
{
_position++;
return true;
}
return false;
} public void Reset()
{
_position = -1;
} #endregion #endregion
}

算是比较简单的 List 的实现,暂时先这样,有些地方还得完善,
知识点 : 索引器,枚举器,其他的都比较简单

【数据结构】 List 简单实现的更多相关文章

  1. 堆数据结构(heapq)简单应用

    ## 堆数据结构(heapq)简单应用 # 堆数据结构 heapq # 常用方法:nlargest(),nsmallest(),heapify(),heappop() # 如果需要的个数较小,使用nl ...

  2. Redis数据结构之简单动态字符串SDS

    Redis的底层数据结构非常多,其中包括SDS.ZipList.SkipList.LinkedList.HashTable.Intset等.如果你对Redis的理解还只停留在get.set的水平的话, ...

  3. 单链表数据结构 - java简单实现

    链表中最简单的一种是单向链表,每个元素包含两个域,值域和指针域,我们把这样的元素称之为节点.每个节点的指针域内有一个指针,指向下一个节点,而最后一个节点则指向一个空值.如图就是一个单向链表 一个单向链 ...

  4. 【redis】redis底层数据结构原理--简单动态字符串 链表 字典 跳跃表 整数集合 压缩列表等

    redis有五种数据类型string.list.hash.set.zset(字符串.哈希.列表.集合.有序集合)并且自实现了简单动态字符串.双端链表.字典.压缩列表.整数集合.跳跃表等数据结构.red ...

  5. 【数据结构】简单谈一谈二分法和二叉排序树BST查找的比较

    二分法查找: 『在有序数组的基础上通过折半方法不断缩小查找范围,直至命中或者查询失败.』   二分法的存储要求:要求顺序存储,以便于根据下标随机访问   二分法的时间效率:O(Log(n))   二分 ...

  6. C语言数据结构之 简单选择排序

    算法:设所排序序列的记录个数为n.i取1,2,-,n-1,从所有n-i+1个记录(Ri,Ri+1,-,Rn)中找出排序码最小的记录,与第i个记录交换.执行n-1趟 后就完成了记录序列的排序. 编译器: ...

  7. Java数据结构系列——简单排序:泡、选择、直接进入

    package SimpleSort; public class SimpleSort { /** * 冒泡排序:每次循环过程中.小的排在后面的数会像水中的 * 气泡一样慢慢往上冒,所以命名为冒泡排序 ...

  8. (hdu step 8.1.6)士兵队列训练问题(数据结构,简单模拟——第一次每2个去掉1个,第二次每3个去掉1个.知道队伍中的人数&lt;=3,输出剩下的人 )

    题目: 士兵队列训练问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...

  9. redis 系列3 数据结构之简单动态字符串 SDS

    一.  SDS概述 Redis 没有直接使用C语言传统的字符串表示,而是自己构建了一种名为简单动态字符串(simple dynamic string, SDS)的抽象类型,并将SDS用作Redis的默 ...

  10. OI数据结构&&分治 简单学习笔记

    持续更新!!! [例题]简单题(K-D tree) 题目链接 线段树 [例题](环上最大连续和) 给定一个长度为n的环形序列A,其中A1与A_n是相临的,现在有q次修改操作,每次操作会更改其中一个数, ...

随机推荐

  1. 从零开始Vue项目实战(一)-准备篇

    从前参与过一个react项目的代码编写,大神搭建的框架,我主要负责业务逻辑代码编写,现在回想起来似乎又什么都不会,现在为了巩固前端知识,决定用Vue来做这个项目的移动端网站,我本人Vue是从零开始的, ...

  2. 成都夏季招聘会IT行业缺口大!

    上个周末成都的夏季招聘会在新会展中心举行,我们传智播客的专业市场调查员也深入当中.了解IT行业招聘情况,我们发如今IT软件行业专区招聘的公司特别多,可是去应聘的人却非常少.这意味着IT行业正处于供不应 ...

  3. Jupyter notebook远程访问linux服务器

    [转]https://blog.csdn.net/akon_wang_hkbu/article/details/78973366

  4. 【[JLOI2014]松鼠的新家】

    //第一次A掉紫题就来写题解,我是不是疯了 //说实话这道题还是比较裸的树上差分 //对于树上的一条路径(s,t),我们只需要把ch[s]++,ch[t]++,ch[LCA(S,T)]--,再把lca ...

  5. CodeForces-822D 【最小素因子应用】

    任意门:https://vjudge.net/problem/CodeForces-822D D. My pretty girl Noora time limit per test 1.5 secon ...

  6. 对selenium自动化框架重构

    近期在编写自动化脚本的时候发现以前写的部分不是特别友好,在此进行重构.废话不说 搭建自动化框架,首先先理清思路: 使用的技术python+unittest+selenium+excel 文件目录有: ...

  7. 2018.11.21 struts2获得servletAPI方式及如何获得参数

    访问servletAPI方式 第一种:通过ActionContext (重点及常用 都是获得原生对象) 原理 Action配置 被引入的配置文件 在页面调用取值 第二种:通过ServletAction ...

  8. 优雅的QSignleton (五) 优雅地进行GameObject命名

      这段时间空调吹感冒了,休息了好久 ​ 本篇介绍QSingleton最重要的功能,是它让QSingleton称得上优雅.相关内容之前介绍过. 代码如下: MonoSingletonPath.cs n ...

  9. 名词解释-FrameWork

    直接翻译的意思是架构,但这样说可能不懂,下面我从两个方面来给你说吧: 一是比喻来说,假设你现在要盖楼房,framework就好比一个建筑公司,它里面有专门采集石料的,专门的租夹板的,专门的磨砂,搬砖的 ...

  10. MySQL提升课程 全面讲解MySQL架构设计

    1:并发量:同一时间处理请求数量,同一时间处理请求数量和连接数量是两个概念,连接数大于处理请求数量, MySQL参数最大连接数max_connections 这是是查询数据库当前设置的最大连接数 my ...