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. Python:运算与循环

    1.格式化输出 name = input("请输入你的名字:") age =input("请输入你的年龄:") job =input("请输入你的工作 ...

  2. Gym - 101246D 博弈

    题意:一个无向有环的图,从 1 号结点起火,开始蔓延,两个绝顶聪明的人轮流走,谁不能走谁输,输出输的人: 分析: 当时知道是博弈,但是想当然的以为 1 号结点有一个奇数层,就必胜:其实不是这样的,当一 ...

  3. 【[CTSC2000]冰原探险】

    noip前练一下码力还是非常有前途的 这道题本来就是想写个大暴力弃疗的,所以直接强上暴力浑身舒爽 结果发现要不是判重的时候脑残了,就能\(A\)了 没什么好说的呀,就是每一次都暴力\(O(n)\)往上 ...

  4. 【转】总结oninput、onchange与onpropertychange事件的用法和区别

    经本人测试在chrome下的从历史记录中选取值的时候也户触发input事件 前端页面开发的很多情况下都需要实时监听文本框输入,比如腾讯微博编写140字的微博时输入框hu9i动态显示还可以输入的字数.过 ...

  5. (第四场)F Beautiful Garden

    题目: F Beautiful Garden 题目描述 There's a beautiful garden whose size is n x m in Chiaki's house. The ga ...

  6. umlの类图

    版权声明:本文为博主原创文章,若要转载请注明出处!^_^ https://blog.csdn.net/u010892841/article/details/24844825 类图class diagr ...

  7. Css 截取字符串长度

    .shortNameShow{ overflow:hidden; text-overflow:ellipsis; -o-text-overflow:ellipsis; white-space:nowr ...

  8. 【OJ-UVa227】

    耗时一周.哭. 本题重在输入输出.所以对英文题目的理解非常重要.看清楚题目,省时省力. 题目要点: 1.开始有5×5的数据,每行仅有5个字符.注意:样例输入中的尾部空格是无法复制的(UVa官网上),其 ...

  9. UGUI富文本

    <b>text</b>  --粗体 <i>text<i>     --斜体 <size=10>text</size>   --自 ...

  10. [USACO1.5]数字三角形 Number Triangles

    题目描述 观察下面的数字金字塔. 写一个程序来查找从最高点到底部任意处结束的路径,使路径经过数字的和最大.每一步可以走到左下方的点也可以到达右下方的点. 7 3 8 8 1 0 2 7 4 4 4 5 ...