现在的位置: 首页 > 综合 > 正文

数据结构(C#语言版)——线性表

2011年12月14日 ⁄ 综合 ⁄ 共 2404字 ⁄ 字号 评论关闭

线性表

线性表(List):由n(n>=0)个相同类型的数据元素构成的有限序列。
简记L=(D,R) D:数据元素的有限集合;R:数据元素之间关系的有限集合。
线性表的基本操作:求长度、清空操作、判断线性表是否为空、附加操作、插入操作、删除操作、取表元。

   1: /// <summary>

   2: /// 线性表的基本操作接口

   3: /// 线性表(List):由n(n>=0)个相同类型的数据元素构成的有限序列。

   4: /// 简记L=(D,R) D:数据元素的有限集合;R:数据元素之间关系的有限集合。

   5: /// </summary>

   6: /// <typeparam name="T">元素类型</typeparam>

   7: public interface IList<T>: IEnumerable<T> {

   8:     /// <summary>

   9:     /// 列表长度

  10:     /// </summary>

  11:     int Count { get; }

  12:  

  13:     /// <summary>

  14:     /// 清空列表

  15:     /// </summary>

  16:     void Clear();

  17:  

  18:     /// <summary>

  19:     /// 判断是否为空

  20:     /// </summary>

  21:     bool IsEmpty { get; }

  22:  

  23:     /// <summary>

  24:     /// 添加一个数据项至列表尾部

  25:     /// </summary>

  26:     /// <param name="value">插入项</param>

  27:     void Add(T value);

  28:  

  29:     /// <summary>

  30:     /// 在指定位置插入一个数据项

  31:     /// </summary>

  32:     /// <param name="value">数据项</param>

  33:     /// <param name="index">位置</param>

  34:     void Insert(T value, int index);

  35:  

  36:     /// <summary>

  37:     /// 删除指定位置的数据项

  38:     /// </summary>

  39:     /// <param name="index">位置</param>

  40:     void Delete(int index);

  41:  

  42:     /// <summary>

  43:     /// 获取指定位置的数据项

  44:     /// </summary>

  45:     /// <param name="index">位置</param>

  46:     /// <returns>数据项</returns>

  47:     T this[int index] { get; set; }

  48: }

顺序表

在内存中用一块地址连续的空间依次存放线性表的数据元素,这种方式存储的线性表叫顺序表。

顺序表是用地址连续的存储单元顺序存储线性表中的各个数据元素,逻辑上相邻的数据元素在物理位置上也相邻。因此,在顺序表中查找任何一个位置上的数据元素非常方便,这是顺序存储的优点。但是,在对顺序表进行插入和删除时,需要通过移动数据元素来实现 ,影响了运行效率。

顺序表实现:

   1: public class SequenceList<T> : IList<T> {

   2:     private const int Step = 16;

   3:     private int _capacity;

   4:     private T[] _datas;

   5:     private int _last = -1;

   6:  

   7:     public SequenceList() : this(Step) {}

   8:  

   9:     public SequenceList(int capacity) {

  10:         this._capacity = capacity;

  11:         this._datas = new T[capacity];

  12:     } 

  13:  

  14:     #region Implementation of IEnumerable

  15:  

  16:     public IEnumerator<T> GetEnumerator() {

  17:         int index = 0;

  18:         while(index<=this._last) {

  19:             yield return this._datas[index++];

  20:         }

  21:     }

  22:  

  23:     IEnumerator IEnumerable.GetEnumerator() {

  24:         return GetEnumerator();

  25:     }

  26:  

  27:     #endregion

  28:  

  29:     #region Implementation of IList<T>

  30:  

  31:     /// <summary>

  32:     /// 列表长度

  33:     /// </summary>

  34:     public int Count {

  35:         get { return this._last + 1; }

  36:     }

  37:  

  38:     /// <summary>

  39:     /// 清空列表

  40:     /// </summary>

  41:     public void Clear() {

  42:         this._last = -1;

  43:     }

  44:  

  45:     /// <summary>

  46:     /// 判断是否为空

  47:     /// </summary>

  48:     public bool IsEmpty {

  49:         get { return this._last == -1; }

  50:     }

  51:  

  52:     /// <summary>

  53:     /// 添加一个数据项至列表尾部

  54:     /// </summary>

  55:     /// <param name="value">插入项</param>

  56:     public void Add(T value) {

  57:         if(this._last+1==this._capacity) {

  58:             Reassign();

  59:         }

  60:         this._last++;

  61:         this._datas[this._last] = value;

  62:     }

  63:  

  64:     private void Reassign() {

  65:         this._capacity += Step;

  66:         T[] newDatas = new T[this._capacity];

  67:         int index = 0;

  68:         while(index<=this._last) {

抱歉!评论已关闭.