码迷,mamicode.com
首页 > Windows程序 > 详细

第三章 线性表(C#实现)

时间:2014-11-22 17:12:51      阅读:442      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   sp   for   strong   数据   

1、线性表

  概念::零个或多个数据元素的有序序列。

  描述:

bubuko.com,布布扣

2、线性表的抽象数据类型:

  ADT线性表

  Data:线性表的数据对象集合为{a1,a2,...,an},每个元素的类型均为DataType。其中除第一个元素a1外,每一个元素有且只有一个直接前驱元素,除了最后一个元素an外,每一个元素有且只有一个直接后继元素,数据元素之间的关系是一对一的关系。

  Operation

  Init(*L):初始化操作,建立一个空的线性表;

  IsEmpty(*L):判断线性表是否为空,若为空,则返回true,否则返回false;

  Clear(*L):清空线性表;

  GetElem(*L,i):返回线性表位置为i的元素;

  LocateElem(*L,e):返回线性表中与元素i相同值的位置;

  Insert(*L,i,e):在线性表i位置插入元素e;

  Delete(*L,i):删除线性表位置i的元素;

  GetLength(*L):获得当前线性表的长度。

endADT

 

3、线性表的顺序存储结构

  概念:用一段地址连续的存储单元存储线性表的数据元素。

  如图:

    bubuko.com,布布扣

 

    算法的C#语言实现

    

  /// <summary>
    /// 线性表的顺序存储结构
    /// </summary>
    public class SequentialStorageLinearList<T>
    {
        //最大存放数量
        private int _maxLength;

        /// <summary>
        /// 最大存储数量
        /// </summary>
        public int MaxLength
        {
            get
            {
                return _maxLength;
            }
        }

        // 当前存放位置
        private int _currentLength;

        private T[] _LinearList;

        //利用构造函数创建一个数组,用来存放数据
        public SequentialStorageLinearList(int maxLength)
        {
            if (maxLength <= 0)
                throw new Exception("The length can not be less than zero.");
            _maxLength = maxLength;
            _currentLength = 0;
            _LinearList = new T[_maxLength];
        }

        /// <summary>
        /// 判断线性表是否为空
        /// </summary>
        /// <returns></returns>
        public bool IsEmpty()
        {
            return _currentLength == 0;
        }

        /// <summary>
        /// 清空所有的元素
        /// </summary>
        public void ClearAll()
        {
            if (_currentLength > 0)
            {
                /*
                 * 在高级语言.net中,我们可以重新创建一个新的数组,将其指向当前的_linearList
                 *由于是托管代码,资源不需要手动释放,会自动释放
                 *但是在c语言中,我们需要手动的释放其占用的空间(free)
                 */
                _LinearList = new T[0];
            }
        }

        /// <summary>
        /// 根据
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        public T GetElement(int i)
        {
            if (_currentLength == 0 || _currentLength < i || i < 1)
                throw new Exception("Not find any Element.");
            T t = _LinearList[i];
            return t;
        }

        /// <summary>
        /// 查找数据中是否存在元素e
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public int IndexOf(T e)
        {
            if (_currentLength == 0)
                throw new Exception("The array is empty.");
            for (int i = 0; i < _currentLength; i++)
            {
                T t = _LinearList[i];
                /*
                 * T可能是引用类型,我们暂时的比较方式,不考虑重写equals
                 */
                if (t.Equals(e))
                {
                    return i;
                }
            }
            return -1;
        }

        /// <summary>
        /// 在位置i处插入元素e
        /// </summary>
        /// <param name="e">待插入的元素e</param>
        /// <param name="i">插入位置i</param>
        public void Insert(T e, int i)
        {
            //数组已经存满
            if (_currentLength == _maxLength)
                throw new Exception("The array is full.");
            //当i位置不在范围内
            if (i < 1 || i > _currentLength + 1)
                throw new Exception("The location is illegal.");
            //若插入的数据不在表尾,在需要移动插入位置后面的数据一位
            if (i <= _currentLength - 1)
            {
                for (int j = _currentLength - 1; j > i; j--)
                {
                    _LinearList[j + 1] = _LinearList[j];
                }
            }
            _LinearList[i - 1] = e;
            _currentLength++;
        }

        /// <summary>
        /// 删除位置i处的元素
        /// </summary>
        /// <param name="i">位置i</param>
        public void Delete(int i)
        {
            if (_currentLength == 0)
                throw new Exception("The array is Empty.");
            if (i < 1 || i > _currentLength)
                throw new Exception("The location is illegal.");
            //删除位置i元素后需要将i后面的元素依次递增一位
            if (i < _currentLength)
            {
                for (int k = k-1; k < _currentLength; k++)
                {
                    _LinearList[k - 1] = _LinearList[k];
                }
            }
            _currentLength--;
        }
    }

  

第三章 线性表(C#实现)

标签:style   blog   http   io   ar   sp   for   strong   数据   

原文地址:http://www.cnblogs.com/xiuyuanjing/p/4115254.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!