线性表(Linear List)是由n(n≥0)个数据元素(结点)a[0],a[1],a[2]…,a[n-1]组成的有限序列。.Net Framework的System.Collection命名空间内并没有提供对线性表的太多支持,只提供了ArrayList类似与顺序表。Snake.Net提供三种形式的线性表,顺序表,单向链表和双向链表分别对应类Eastasp.Framework.Collections.OrderedTable、Eastasp.Framework.Collections.SingleLink和Eastasp.Framework.Collections.DoubleLink。
先来了解一下三种表的概念:
顺序表是在计算机内存中以数组的形式保存的线性表,是指用一组地址连续的存储单元依次存储数据元素的线性结构。
单向链表是链表的一种,其特点是链表的链接方向是单向的,对链表的访问要通过顺序读取从头部开始。
双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱。所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点。
下面的代码中了解一下Snake.Net中的线性表
2{
3 #region namespace
4 using System;
5 using System.Collections;
6 using Collections;
7 using Diagnostics;
8 using Utility;
9 using NUnit.Framework;
10 #endregion
11
12 #region class for CollectionTest
13 /// <summary>
14 /// Summary description for CollectionTest.
15 /// </summary>
16 [TestFixture]
17 public class CollectionTest:ITest
18 {
19 public CollectionTest()
20 {
21 }
22
23 [Test]
24 public void Test()
25 {
26 OutputLinks();
27 }
28
29 private void OutputLinks()
30 {
31 //declare
32 ILink[] links;
33
34 //output start infomation
35 Console.Write("------- Starttest Links -------{0}{0}{0}", StringUtil.CrLf);
36
37 links = new ILink[]{
38 new OrderedTable(),
39 new SingleLink(),
40 new DoubleLink()};
41
42 for(int i = 0; i < links.Length; i++){
43 Console.Write("Start Test {0} {1}", links[i].GetType().FullName, StringUtil.CrLf);
44 OutputLink(links[i]);
45 Console.Write("Test Completed{1}{1}", links[i].GetType().FullName, StringUtil.CrLf);
46 }
47
48 //output end infomation
49 Console.Write("{0}{0}------- End test Links -------{0}{0}", StringUtil.CrLf);
50 }
51
52
53 private void OutputLink(ILink link)
54 {
55 //declare
56 object[] array;
57 DateTime start;
58 DateTime end;
59 TimeSpan passed;
60
61 start = DateTime.Now;
62 Console.Write(String.Format("Start date time:{0}{1}", start.ToString("MM/dd/yyyy HH:mm:ss"), StringUtil.CrLf));
63
64 for(int i = 0; i < 10000; i++){
65
66 link.Clear();
67
68 //initialize
69 array = new object[]{"aaa", "bbb", "ccc", "ddd", "eee", "hhh", "fff", "ggg", "bb2", "cc2", "dd2", "ee2", "hh2", "ff2", "gg2", "iii", "jjj", "kkk"};
70 link.AddRange(array);
71 Assert.AreEqual(link.Count, array.Length, "Error!");
72
73 link.Add("000");
74 Assert.AreEqual(link.Count, array.Length + 1, "Error!");
75
76 link.Remove("ddd");
77 Assert.AreEqual(link.Count, array.Length, "Error!");
78
79 link.Insert(3, "222");
80 Assert.AreEqual(link.Count, array.Length + 1, "Error!");
81
82 link.RemoveAt(2);
83 Assert.AreEqual(link.Count, array.Length, "Error!");
84 }
85
86 end = DateTime.Now;
87 passed = new TimeSpan(end.Ticks - start.Ticks);
88 Console.Write(String.Format("End date time:{0}{1}", end.ToString("MM/dd/yyyy HH:mm:ss"), StringUtil.CrLf));
89 Console.Write(string.Format("spend {0} seconds, {1} milliseconds {2}", passed.Seconds, passed.Milliseconds, StringUtil.CrLf));
90 }
91 }
92 #endregion
93}
运行结果如下:
------- Starttest Links -------
Start Test Eastasp.Framework.Collections.OrderedTable
Start date time:07-21-2005 09:58:16
End date time:07-21-2005 09:58:16
spend 0 seconds, 812 milliseconds
Test Completed
Start Test Eastasp.Framework.Collections.SingleLink
Start date time:07-21-2005 09:58:16
End date time:07-21-2005 09:58:17
spend 0 seconds, 765 milliseconds
Test Completed
Start Test Eastasp.Framework.Collections.DoubleLink
Start date time:07-21-2005 09:58:17
End date time:07-21-2005 09:58:18
spend 0 seconds, 765 milliseconds
Test Completed
------- End test Links -------