首页 > 学院 > 开发设计 > 正文

C#顺序表

2019-11-17 02:35:39
字体:
来源:转载
供稿:网友

C#顺序表

1 数据是外部世界的载体,能够被计算机处理加工。2数据元素是数据的基本单位,如,学生的姓名、性别、班级等等。

3数据对象是数据的一个集合,如 人类,狗类,等等属于数据对象,对象里又有数据元素

4数据类型,如整形,字符串,结构体等等,

5.数据结构通常分为4种,5.1集合 如HashSet<T>类主要是设计用来做高性能集运算的,(交集、并集、差集)

后续再谈HashSet<>.

5.2线性结构 数据元素从在一对一的关系 如 数组、list<>、Stack<>、Queue<>

5.3树形结构,如二叉树等等。图形结构,以后再说

第一天主要学习顺序表,保存线性表的方式就是把表中的元素一个接一个的放进顺序的存储单元,这就是线性表的顺序存储。在内存中用一块地址连续的存储空间一次存放数据元素,称为顺序表。如图

//自己实现顺序表    public class SeqList<T> //: IList<T>    {        PRivate int maxsize; //顺序表的容量        public int Maxsize        {            get { return maxsize; }        }        private T[] data; //数组,用于存储顺序表中的数据元素        private int last; //指示顺序表最后一个元素的位置下标        public int Last        {            get { return last; }            set { last = value; }        }        // 索引器        public T this[int index]        {            get { return data[index]; }            set { data[index] = value; }        }        public SeqList( int size )        {            data = new T[size];            maxsize = size;            last = - 1;        }        // 顺序表的长度由于数组是 0 基数组,即数组的最小索引为 0,所以,顺序表的长度就是数        // 组中最后一个元素的索引 last 加 1。        public int GetLength()        {            return last + 1;        }        // 清空        public void Clear( )        {            last = -1;        }        public bool IsEmpty( )        {            if ( last < 0 )            {                return true;            }            return false;        }        // 判断顺序表是否已满        public bool IsFull( )        {            if ( last == maxsize - 1 )// 元素下标从0开始            {                return true;            }            return false;        }        // 在顺序表末尾添加新元素        public void Appled(T item)        {            if ( !IsFull( ) )            {                data[++last] = item;            }            else            {                Console.WriteLine(" 顺序表已满无法添加 ");            }        }        //在顺序表的第i个数据元素的位置插入一个数据元素        /*            算法的时间复杂度分析:顺序表上的插入操作,时间主要消耗在数据的移动        上,在第i个位置插入一个元素,从ai到an都要向后移动一个位置,共需要移动n-i+1        个元素,而i的取值范围为 1≤i≤n+1,当i等于 1 时,需要移动的元素个数最多,        为n个;当i为n+1 时,不需要移动元素。设在第i个位置做插入的概率为pi,则平        均移动数据元素的次数为n/2。这说明:在顺序表上做插入操作平均需要移动表        中一半的数据元素,所以,插入操作的时间复杂度为O(n)。         */        public void Insert(T item, int i)        {            if ( i < 1 || i > last + 1 )            {                Console.WriteLine(" 位置非法 ");                return;            }            if ( IsFull() )            {                Console.WriteLine(" 顺序表已满无法添加 ");                return;            }            if (i == last + 2)// 判断是不是末尾插入            {                data[ last + 1 ] = item;            }            else            {                // i后面的元素往后移                for (int j = last; j >= i - 1; --j  )                {                    data[j + 1] = data[j];                }                //将新的数据元素插入到第i-1个位置上                data[i - 1] = item;             }            ++last;        }        //删除顺序表的第i个数据元素        /*         而i的取值范围为 1≤i≤n,当i等于 1 时,需要移动        的元素个数最多,为n-1 个;当i为n时,不需要移动元素。设在第i个位置做删除        的概率为pi,则平均移动数据元素的次数为(n-1)/2。这说明在顺序表上做删除操        作平均需要移动表中一半的数据元素,所以,删除操作的时间复杂度为O(n)         */        public T Delete(int i)        {            T temp = default(T);// default 为泛型代码中的默认关键字            if ( IsEmpty( ) )            {                Console.WriteLine("顺序表为空");                return temp;            }            if (i < 1 || i > last + 1 )            {                Console.WriteLine(" 位置非法 ");                return temp;            }            if ( i == last + 1 )            {                data[last--] = temp;            }             else            {                temp = data[i - 1];                for (int j = i; j <= last; ++j )                {                    data[j] = data[j - 1];                }            }            --last;            return temp;        }        //获得顺序表的第i个数据元素        public T GetElem(int i)       {             if ( IsEmpty( ) || (i < 1) || ( i > last + 1 ) )             {                 Console.WriteLine("顺序表为空 or 位置非法!");                 return default(T);             }             return data[i-1];         }         //在顺序表中查找值为value的数据元素        public int Locate(T value)        {            if (IsEmpty( ))            {                Console.WriteLine("顺序表为空");                return -1;            }            for (int j = 0; j < last; ++j )            {                if ( data[j].Equals( value) )                {                    return j;                }            }            return -1;        }        /*         算法的时间复杂度分析:顺序表中的按值查找的主要运算是比较,比较的次        数与给定值在表中的位置和表长有关。当给定值与第一个数据元素相等时,比较        次数为 1;而当给定值与最后一个元素相等时,比较次数为 n。所以,平均比较        次数为(n+1)/2,时间复杂度为 O(n)         */        public void Reverse()        {            T temp = default(T);            int len = GetLength();            for (int i = 0; i <= len >> 1; ++i)            {                temp = data[i];                data[i] = data[len - i];                data[len - i] = temp;            }        }    }

List<T>他是表示通过索引访问的对象的强制类型列表。提供搜索、排序、插入、删除等操做

通过学习线性表我们知道了,List<T>实现了顺序表的功能,但List<T>是一个动态存储数据的链表,这就是顺序表的缺点,明天我们学习单链表

本人希望和各位较多,互相想学习,下面简单介绍List<>

作用:泛型最常见的用途是泛型集合我们在创建列表类时,列表项的数据类型可能是int,string或其它类型,如果对列表类的处理方法相同,就没有必要事先指定数据类型,留待列表类实例化时再指定。相当于把数据类型当成参数,这样可以最大限度地重用代码,保护类型的安全以及提高性能。 List的一般用法所属命名空间: System.Collections.Genericpublic class List<T>:IList<T>,Icollection<T>,IEnumerable<T>,IList,Icollection,IenumerableList<T>是ArrayList类的泛型等效类,该类使用大小可按需动态增加的数组实现IList<T>泛型接口 (1)声明 List<T>mlist = new List<T>(); eg: string[] Arr = {"a","b","c"};     List<string> mlist = new List<string>(Arr); (2)添加一个元素 List.Add(T item)    eg: mlist.Add("d"); (3)添加集合元素   eg: string[] Arr2 ={"f","g"."h"};       mlist.AddRange(Arr2); (4)在index位置添加一个元素 Insert(int index,T item)   eg: mlist.Insert(1,"p"); (5)遍历List中元素  foreach(T element in mlist) T的类型与mlist声明时一样     {       Console.WriteLine(element);          }    eg:    foreach(string s in mlist)          {             Console.WriteLine(s);           } (6)删除元素    List.Remove(T item) 删除一个值    eg: mlist.Remove("a");    List.RemoveAt(int index);删除下标为index的元素    eg: mlist.RemoveAt(0);        List.RemoveRange(int index,int count); 下标index开始,删除count个元素    eg:mlist.RemoveRange(3,2);  (7)判断某个元素是否在该List中    List.Contains(T item) 返回true或false    eg:    if(mlist.Contains"("g"))       Console.WriteLine("g存在列表中");    else       mlist.Add("g"); (8)给List里面元素排序 List.Sort() 默认是元素每一个字母按升序   eg: mlist.Sort(); (9)给List里面元素顺序反转 List.Reverse() 可以与List.Sort()配合使用 (10)List清空 List.Clear()   eg: mlist.Clear();(11)获得List中元素数目 List.Count() 返回int值   eg: mlist.count(); List进阶,强大方法 (1)List.FindAll方法:检索与指定谓词所定义的条件相匹配的所有元素         class program    {       static void Main(stirng[] args)       {         student stu = new student();         stu.Name="arron";         List<student> students= new List<student>();         students.Add(stu);         students.Add(new student("candy"));         FindName myname = new FindName("arron");         foreach(student s in students.FindAll(new Predicate<student>(myname.IsName)))         { Console.WriteLine(s);}       }    public class student    {       public string Name{get;set;}       public student(){}       public override string ToString()        {            return string.Format("姓名:{0}",Name);         }     }    public class FindName    {      private string _name;      public FindName(string Name)      {  this._name=Name;}      public bool IsName(student s)       { return (s.Name==_name)?true:false;}    } (2)List.Find方法 搜索与指定谓词所定义的条件相匹配的元素,并返回整个List中的第一个匹配元素  eg:    //Predicate是对方法的委托,如果传递给它的对象与委托定义的条件匹配,则该方法返回true,当前List的元素  被逐个传递给Predicate委托,并在List中间前移动,从第一个元素开始,到最后一个元素结束,当找到匹配项  时处理停止  第一种方法 委托给拉姆达表达式:  eg:       string listFind = mlist.Find(name=>       {          if(name.length>3)             return true;          return false;       });    第二种方法 委托给一个函数    eg:
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表