数据结构解析与归纳

本文对常用的数据结构:Array, ArrayList,List,IList,ICollection, Stack, Queue, HashTable, Dictionary, IQueryable, IEnumerable等进行详述。


一、Collection(集合)
Collection是数据记录集合, 编写代码过程中,常常需要合适的容器保存临时数据,方便修改和查找,如何选取合适的数据容器,关键在于将执行的数据操作以及数据记录是否大量。


二、Array(数组)

特征

1. 固定大小,数组的大小是初始化时决定无法修改的数值。

2. 强类型,存储数据元素类型必须在初始化时指定,因此在运行时,不需要耗费额外的时间来定义数组类型,能够大大提升运行效率。

3. 可使用Foreach关键字实现数组迭代和查找。

因为数组大小是固定的,且是强类型数据结构,因此在运行时只占用很少的内存,运行时效率很高。


三、ArrayList

1. ArrayList 没有固定的长度,容量可动态增加,可应用于开发人员无法确定数组元素个数等场景,当然这种情况下,在定义结构体的时候会非常耗时。

2. ArrayList 不是强类型,ArrayList中不同元素类型可以不相同,并且需要在运行时根据实际的输入来确定元素类型。因此在运行时消耗内存较多。

3. 可使用Froeach 关键字操作ArrayList。

ArrayList支持String,int,以及十进制小数类型。


四、HashTable(哈希表)

HashTable是一种定义关键字的数据结构体,使用哈希表查找数据非常方便,哈希表既不是强类型也不固定大小限制。


五、Stack

栈是最典型的数据结构,栈具有优先级划分的数据结构,栈为每个内容项定义优先级,表示每个Item入栈和出栈的优先顺序。因此操作栈中的数据,需要先将数据push 到栈的顶部,需要删除元素必须变成栈顶部,即要遵守后进先出(LIFO)的原则。

栈与哈希表一样既不是强类型也不限制元素个数。

Push 操作

  1. //Stack is LIFO: Last in First Out
  2. System.Collections.Stack objStackPush = new System.Collections.Stack();

  3. //By Push method you can insert item at the top of the stack
  4. objStackPush.Push("Mahsa");
  5. objStackPush.Push("Hassankashi");
  6. this.lblPop.Text = "";
  7. this.ListBoxStack.DataSource = objStackPush.ToArray();
  8. this.ListBoxStack.DataBind();
复制代码

Pop操作

  1. System.Collections.Stack objStackPop = new System.Collections.Stack();
  2.    
  3. objStackPop.Push("Mahsa");
  4. objStackPop.Push("Hassankashi");

  5. //By Pop method you can remove item from the top of the stack --> Last in First in
  6. this.lblPop.Text = objStackPop.Pop().ToString();

  7. this.ListBoxStack.DataSource = objStackPop.ToArray();
  8. this.ListBoxStack.DataBind();
复制代码

六、Queue

Queue同栈一样也是具有优先级定义的结构体,遵循的规则是先进先出(FIFO),既不是强类型也不具有固定的大小限制。

入队操作

  1. //Queue is FIFO: First in First Out
  2. System.Collections.Queue objQueue = new System.Collections.Queue();

  3. //By Enqueue method you can insert item at the END of the Queue
  4. objQueue.Enqueue("Mahsa");
  5. objQueue.Enqueue("Hassankashi");
  6. objQueue.Enqueue("Cosmic");
  7. objQueue.Enqueue("Verse");

  8. this.lblQueue.Text = "";
  9. this.ListBoxQueue.DataSource = objQueue.ToArray();
  10. this.ListBoxQueue.DataBind();
复制代码
出队操作
  1. System.Collections.Queue objQueue = new System.Collections.Queue();

  2. objQueue.Enqueue("Mahsa");
  3. objQueue.Enqueue("Hassankashi");
  4. objQueue.Enqueue("Cosmic");
  5. objQueue.Enqueue("Verse");

  6. //By Dequeue method you can remove item from the BEGINING of the Queue --> First in First out FIFO
  7. this.lblQueue.Text=objQueue.Dequeue().ToString();

  8. this.ListBoxQueue.DataSource = objQueue.ToArray();
  9. this.ListBoxQueue.DataBind();
复制代码

入队操作

  1. System.Collections.Queue objQueue = new System.Collections.Queue();

  2. objQueue.Enqueue("Mahsa");
  3. objQueue.Enqueue("Hassankashi");
  4. objQueue.Enqueue("Cosmic");
  5. objQueue.Enqueue("Verse");

  6. //By Dequeue method you can remove item from the BEGINING of the Queue --> First in First out FIFO
  7. this.lblQueue.Text=objQueue.Dequeue().ToString();

  8. this.ListBoxQueue.DataSource = objQueue.ToArray();
  9. this.ListBoxQueue.DataBind();
复制代码

七、List

什么情况下需要使用List?

1. List长度可不固定

2. 当数据为通用类型,List是强类型,List中元素类型不需要等到运行时来确定,这种特性使得List 运行时效率非常高。

3. 可使用Foreach关键字。

因为List不需要设定固定的大小,List灵活度高,且效率高常用于开发过程中。

  1. //Like Array is Strong Type
  2. //Like ArrayList with No Dimension
  3. System.Collections.Generic.List<string> strList = new List<string>();

  4. strList.Add("Mahsa");
  5. strList.Add("Hassankashi");
  6. strList.Add("Cosmic");
  7. strList.Add("Verse");

  8. this.ListBoxListGeneric.DataSource = strList;
  9. this.ListBoxListGeneric.DataBind();
  10.   
  11. System.Text.StringBuilder str = new System.Text.StringBuilder();

  12. foreach (var item in strList)
  13. {
  14.    str.Append(" , " + item);
  15. }
  16. this.lblList.Text = str.ToString();
复制代码

八、IList

IList 继承了List,包含多种方法的List接口。如果你无法判断代码改动的可能性,可以使用IList接口,减少模块之间的依赖性。IList是接口因此无法被实例化,所以必须使用List来初始化。

  1. //Ilist can not be instantiate from Ilist , so it should be instantiate from List
  2. System.Collections.Generic.IList<string> strIList = new List<string>();

  3. strIList.Add("Mahsa");
  4. strIList.Add("Hassankashi");
  5. strIList.Add("Cosmic");
  6. strIList.Add("Verse");
  7.   
  8. this.ListBoxListGeneric.DataSource = strIList;
  9. this.ListBoxListGeneric.DataBind();
  10.   
  11. System.Text.StringBuilder str = new System.Text.StringBuilder();

  12. foreach (var item in strIList)
  13. {
  14.     str.Append(" , " + item);
  15. }
  16. this.lblList.Text = str.ToString();
复制代码

我们一起了解一下具体的类和接口之间的区别。

1. 具体类可继承其他类,并实现一个或多个接口。

2. 在内部类中可以定义变量并赋值,接口中不允许此操作。

3. 具体类可包含构造函数,而接口中不能定义构造函数

4. 抽象类中可包含访问修饰符如public,private等,接口中不能包含。


  1. //IEnumerable can not be instantiate from Enumerable , so it should be instantiate from List
  2.      System.Collections.Generic.IEnumerable<Employee> empIEnumerable = new List<Employee>         {   
  3.          new Employee { ID = 1001, Name="Mahsa"},
  4.          new Employee { ID = 1002, Name = "Hassankashi" },
  5.          new Employee { ID = 1003, Name = "CosmicVerse" },
  6.          new Employee { ID = 1004, Name = "Technical" }
  7.      };

  8.       this.GridViewIEnumerable.DataSource = empIEnumerable;
  9.       this.GridViewIEnumerable.DataBind();

  10.       System.Text.StringBuilder str = new System.Text.StringBuilder();

  11.       foreach (Employee item in empIEnumerable)
  12.       {
  13.     str.Append(" , " + item.ID +"-"+item.Name);
  14.       }
  15.      this.lblIEnumerable.Text = str.ToString();
复制代码

九、IEnumerable

IEnumerable常用于遍历集合元素,但是无法修改(删除或添加)数据,使用IEnumberable 会从服务器端将所有数据拷贝到客户端,并进行一定的过滤,如果服务器端有大量数据会造成内存负载超重。

  1. //IEnumerable can not be instantiate from Enumerable , so it should be instantiate from List
  2.      System.Collections.Generic.IEnumerable<Employee> empIEnumerable = new List<Employee>         {   
  3.          new Employee { ID = 1001, Name="Mahsa"},
  4.          new Employee { ID = 1002, Name = "Hassankashi" },
  5.          new Employee { ID = 1003, Name = "CosmicVerse" },
  6.          new Employee { ID = 1004, Name = "Technical" }
  7.      };


  8.       this.GridViewIEnumerable.DataSource = empIEnumerable;
  9.       this.GridViewIEnumerable.DataBind();

  10.       System.Text.StringBuilder str = new System.Text.StringBuilder();

  11.       foreach (Employee item in empIEnumerable)
  12.       {
  13.     str.Append(" , " + item.ID +"-"+item.Name);
  14.       }
  15.      this.lblIEnumerable.Text = str.ToString();
复制代码

十、IQueryable
IQueryable与IEnumberable不同的是,当从服务器端加载过量的数据,IQueryable会自动减少应用负载。IQueryable可保证大数据量时应用程序的高性能。IQueryable会先过滤数据,然后发送给客户端。
  1. DataAccessEntities ctx = new DataAccessEntities();
  2.        var ctx = new DataAccessEntities();
复制代码




十一、SQL Profiler:

如何追踪查询语句生成TSQL,生成需要的数据结构体:

Step 1:
  1. Start -> MS SQL Server 2008 -> Performance Tools -> SQL Server Profiler
复制代码

Step 2:
  1. SQL Server Profiler -> File -> New Trace
复制代码

Step 3:

输入连接数据库的用户名和密码

Step 4:
  1. General (Tab) -> Use the Template: Standard
复制代码

Step 5:
  1. Event Selection (Tab) -> Event : TSQL -> Select : SQL-BatchCompleted | Select Show all Columns

  2. Press Column Filter -> Database Name: Like: "DataAccess"
复制代码

运行

Step 6:

查看结果

Step 7:
生成 IEnumerable数据 :
  1. SELECT 
  2. [Extent1].[ID] AS [ID], 
  3. [Extent1].[Name] AS [Name], 
  4. [Extent1].[Age] AS [Age]
  5. FROM [dbo].[Employee] AS [Extent1]
复制代码

生成 IQueryable :
  1. SELECT 
  2. [Extent1].[ID] AS [ID], 
  3. [Extent1].[Name] AS [Name], 
  4. [Extent1].[Age] AS [Age]
  5. FROM [dbo].[Employee] AS [Extent1]
  6. WHERE 1 = [Extent1].[ID]
复制代码

ICollection 继承了IEnumberable,但是IEnumberable是基于索引的,ICollection不基于索引。

十二、Stack Generic

入栈:

  1. //Stack is LIFO: Last in First Out
  2. //Here is for Push Stack in Generic
  3. //System.Collections.Stack objStackPush = new System.Collections.Stack();
  4. //Stack<T> can be instantiated from Stack<T>

  5. System.Collections.Generic.Stack<int> objStackPush = new System.Collections.Generic.Stack<int>();
  6.   
  7. objStackPush.Push(1);
  8. objStackPush.Push(2);
  9.   
  10. this.lblPopGeneric.Text = "";
  11. this.ListBoxStackGeneric.DataSource = objStackPush.ToArray();
  12. this.ListBoxStackGeneric.DataBind();
复制代码
Queue Generic

入队:

  1. //Stack is LIFO: Last in First Out
  2. //Here is for Pop Stack in Generic
  3. //System.Collections.Stack objStackPop = new System.Collections.Stack();
  4. //Stack<T> can be instantiated from Stack<T>

  5. System.Collections.Generic.Stack<int> objStackPop = new System.Collections.Generic.Stack<int>();

  6. objStackPop.Push(1);
  7. objStackPop.Push(2);

  8. this.lblPop.Text = objStackPop.Pop().ToString();
  9. this.ListBoxStack.DataSource = objStackPop.ToArray();
  10. this.ListBoxStack.DataBind();
复制代码

出队:

  1. //Queue is FIFO: First in First Out
  2. //Here is for Enqueue Queue in Generic
  3. //System.Collections.Queue objQueue = new System.Collections.Queue();
  4. //Queue<T> can be instantiated from Queue<T>
  5. System.Collections.Generic.Queue<int> objQueue = new System.Collections.Generic.Queue<int>();
  6. objQueue.Enqueue(1);
  7. objQueue.Enqueue(2);

  8. this.lblQueue.Text = "";

  9. this.ListBoxQueue.DataSource = objQueue.ToArray();
  10. this.ListBoxQueue.DataBind();
复制代码
十三、Dictionary 及 IDictionary:
Dictionary 可通用,而哈希表不是通用的。Dictionary定义 <TKey,Tvalue>。IDictionary是Dictionary的接口,如果在后期开发中需要大量修改,建议使用IDictionary。
  1. System.Collections.Generic.Dictionary<int, string=""> objDictionary = new Dictionary<int, string="">();

  2. objDictionary.Add(1001, "Mahsa");
  3. objDictionary.Add(1002, "Hassankashi");
  4. objDictionary.Add(1003, "Cosmicverse");

  5. string str = objDictionary[1002];

  6. this.ListBoxDictionary.DataSource = objDictionary;
  7. this.ListBoxDictionary.DataBind();</int,>
复制代码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
数据结构:⼋⼤数据结构分析 数据结构分类 数据结构是指相互之间存在着⼀种或多种关系的数据元素的集合和该集合中数据元素之间的关系组成 。 常⽤的数据结构有:数组,栈,链表,队列,树,图,堆,散列表等,如图所⽰: 线性表和⾮线性表 ⼀、线性表 常见的线性表有:数组、队列、栈、链表 线性表是最基本、最简单、也是最常⽤的⼀种数据结构。线性表(linear list)是数据结构的⼀种,线性表就是数据排列成⼀条先⼀样的结 构,每个线性表上的数据最多只有前和后两个⽅向。⼀个线性表是n个具有相同特性的数据元素的有限序列。 特点: 1. 集合中必存在唯⼀的⼀个"第⼀元素"。 2. 集合中必存在唯⼀的⼀个 "最后元素" 。 3. 除最后⼀个元素之外,均有唯⼀的后继(后件)。 4. 除第⼀个元素之外,均有唯⼀的前驱(前件)。 顺序表⾥⾯元素的地址是连续的;链表⾥⾯节点的地址不是连续的,是通过指针连起来的。 1.数组 数组是可以再内存中连续存储多个元素的结构,在内存中的分配也是连续的,数组中的元素通过数组下标进⾏访问,数组下标从0开始。例 如下⾯这段代码就是将数组的第⼀个元素赋值为 1。 int[] data = new int[100];data[0] = 1; 优点: 1、按照索引查询元素速度快 2、按照索引遍历数组⽅便 缺点: 1、数组的⼤⼩固定后就⽆法扩容了 2、数组只能存储⼀种类型的数据 3、添加,删除的操作慢,因为要移动其他的元素。 适⽤场景: 频繁查询,对存储空间要求不⼤,很少增加和删除的情况。 2.栈 栈是⼀种特殊的线性表,仅能在线性表的⼀端操作,栈顶允许操作,栈底不允许操作。 栈的特点是:先进后出,或者说是后进先出,从栈 顶放⼊元素的操作叫⼊栈,取出元素叫出栈。 栈的结构就像⼀个集装箱,越先放进去的东西越晚才能拿出来,所以,栈常应⽤于实现递归功能⽅⾯的场景,例如斐波那契数列。 3.队列 队列与栈⼀样,也是⼀种线性表,不同的是,队列可以在⼀端添加元素,在另⼀端取出元素,也就是:先进先出。从⼀端放⼊元素的操作称 为⼊队,取出元素为出队,⽰例图如下: 使⽤场景:因为队列先进先出的特点,在多线程阻塞队列管理中⾮常适 ⽤。 4.链表 链表是物理存储单元上⾮连续的、⾮顺序的存储结构,数据元素的逻辑顺序是通过链表的指针地址实现,每个元素包含两个结点,⼀个是存 储元素的数据域 (内存空间),另⼀个是指向下⼀个结点地址的指针域。根据指针的指向,链表能形成不同的结构,例如单链表,双向链表, 循环链表等。 链表的优点: 1. 链表是很常⽤的⼀种数据结构,不需要初始化容量,可以任意加减元素; 2. 添加或者删除元素时只需要改变前后两个元素结点的指针域指向地址即可,所以添加,删除很快; 缺点: 1. 因为含有⼤量的指针域,占⽤空间较⼤; 2. 查找元素需要遍历链表来查找,⾮常耗时。 适⽤场景: 数据量较⼩,需要频繁增加,删除操作的场景 ⼆、⾮线性表 常见的⾮线性表有:树、图、堆 ⾮线性表中数据并不是简单的前后关系。 1.树 树是⼀种数据结构,它是由n(n>=1)个有限节点组成⼀个具有层次关系的集合。把它叫做 "树" 是因为它看起来像⼀棵倒挂的树,也就 是说它是根朝上,⽽叶朝下的。它具有以下的特点: 每个节点有零个或多个⼦节点; 没有⽗节点的节点称为根节点; 每⼀个⾮根节点有且只有⼀个⽗节点; 除了根节点外,每个⼦节点可以分为多个不相交的⼦树; 在⽇常的应⽤中,我们讨论和⽤的更多的是树的其中⼀种结构,就是⼆叉树。 ⼆叉树是树的特殊⼀种,具有如下特点: 每个结点最多有两颗⼦树,结点的度最⼤为2。 左⼦树和右⼦树是有顺序的,次序不能颠倒。 即使某结点只有⼀个⼦树,也要区分左右⼦树。 ⼆叉树是⼀种⽐较有⽤的折中⽅案,它添加,删除元素都很快,并且在查找⽅⾯也有很多的算法优化,所以,⼆叉树既有链表的好处,也有 数组的好处,是两者的优化⽅案,在处理⼤批量的动态数据⽅⾯⾮常有⽤。 2.散列表 散列表,也叫哈希表,是根据关键码和值 (key和value) 直接进⾏访问的数据结构,通过key和value来映射到集合中的⼀个位置,这样就可 以很快找到集合中的对应元素。 记录的存储位置=f(key) 这⾥的对应关系 f 成为散列函数,⼜称为哈希 (hash函数),⽽散列表就是把Key通过哈希函数转换成⼀个整型数字,然后就将该数字对数 组长度进⾏取余,取余结果就当作数组的下标,将value存储在以该数字为下标的数组空间⾥,这块连续存储空间称为散列表或哈希表 (Hash table),这种存储空间可以充分利⽤数组的查找优势来查找元素,所以查找的速度很快。 散列数据结构的性能取决于以下三个因素: 哈希函数 哈希表的⼤⼩ 碰撞处理⽅法 哈希表在应⽤中也是⽐较常见的,就如Java中有些集合类

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值