Snake.Net中的线性表

 

线性表(Linear List)是由nn0)个数据元素(结点)a[0]a[1]a[2]…,a[n-1]组成的有限序列。.Net FrameworkSystem.Collection命名空间内并没有提供对线性表的太多支持,只提供了ArrayList类似与顺序表。Snake.Net提供三种形式的线性表,顺序表,单向链表和双向链表分别对应类Eastasp.Framework.Collections.OrderedTableEastasp.Framework.Collections.SingleLinkEastasp.Framework.Collections.DoubleLink

 

先来了解一下三种表的概念:

顺序表是在计算机内存中以数组的形式保存的线性表,是指用一组地址连续的存储单元依次存储数据元素的线性结构。

 单向链表是链表的一种,其特点是链表的链接方向是单向的,对链表的访问要通过顺序读取从头部开始。

 双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱。所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点。

 下面的代码中了解一下Snake.Net中的线性表



 1 None.gif namespace  Eastasp.Framework.Collections
 2 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 3ExpandedSubBlockStart.gifContractedSubBlock.gif    namespace#region namespace
 4InBlock.gif    using System;
 5InBlock.gif    using System.Collections;
 6InBlock.gif    using Collections;
 7InBlock.gif    using Diagnostics;
 8InBlock.gif    using Utility;
 9InBlock.gif    using NUnit.Framework;
10ExpandedSubBlockEnd.gif    #endregion

11InBlock.gif
12ExpandedSubBlockStart.gifContractedSubBlock.gif    class for CollectionTest#region class for CollectionTest
13ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
14InBlock.gif    /// Summary description for CollectionTest.
15ExpandedSubBlockEnd.gif    /// </summary>

16InBlock.gif    [TestFixture]
17InBlock.gif    public class CollectionTest:ITest
18ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
19InBlock.gif        public CollectionTest()
20ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
21ExpandedSubBlockEnd.gif        }

22InBlock.gif        
23InBlock.gif        [Test]
24InBlock.gif        public void Test()
25ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
26InBlock.gif            OutputLinks();
27ExpandedSubBlockEnd.gif        }

28InBlock.gif        
29InBlock.gif        private void OutputLinks()
30ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
31InBlock.gif            //declare
32InBlock.gif            ILink[] links;
33InBlock.gif
34InBlock.gif            //output start infomation
35InBlock.gif            Console.Write("------- Starttest Links -------{0}{0}{0}", StringUtil.CrLf);
36InBlock.gif
37ExpandedSubBlockStart.gifContractedSubBlock.gif            links = new ILink[]dot.gif{
38InBlock.gif                                   new OrderedTable(), 
39InBlock.gif                                   new SingleLink(), 
40ExpandedSubBlockEnd.gif                                   new DoubleLink()}
;
41InBlock.gif
42ExpandedSubBlockStart.gifContractedSubBlock.gif            for(int i = 0; i < links.Length; i++)dot.gif{
43InBlock.gif                Console.Write("Start Test {0} {1}", links[i].GetType().FullName, StringUtil.CrLf);
44InBlock.gif                OutputLink(links[i]);
45InBlock.gif                Console.Write("Test Completed{1}{1}", links[i].GetType().FullName, StringUtil.CrLf);
46ExpandedSubBlockEnd.gif            }

47InBlock.gif
48InBlock.gif            //output end infomation
49InBlock.gif            Console.Write("{0}{0}------- End test Links -------{0}{0}", StringUtil.CrLf);
50ExpandedSubBlockEnd.gif        }

51InBlock.gif
52InBlock.gif        
53InBlock.gif        private void OutputLink(ILink link)
54ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
55InBlock.gif            //declare
56InBlock.gif            object[] array;
57InBlock.gif            DateTime start;
58InBlock.gif            DateTime end;
59InBlock.gif            TimeSpan passed;
60InBlock.gif
61InBlock.gif            start = DateTime.Now;
62InBlock.gif            Console.Write(String.Format("Start date time:{0}{1}", start.ToString("MM/dd/yyyy HH:mm:ss"), StringUtil.CrLf));
63InBlock.gif
64ExpandedSubBlockStart.gifContractedSubBlock.gif            for(int i = 0; i < 10000; i++)dot.gif{
65InBlock.gif                
66InBlock.gif                link.Clear();
67InBlock.gif                
68InBlock.gif                //initialize
69ExpandedSubBlockStart.gifContractedSubBlock.gif                array = new object[]dot.gif{"aaa""bbb""ccc""ddd""eee""hhh""fff""ggg""bb2""cc2""dd2""ee2""hh2""ff2""gg2""iii""jjj""kkk"};
70InBlock.gif                link.AddRange(array);
71InBlock.gif                Assert.AreEqual(link.Count, array.Length, "Error!");
72InBlock.gif            
73InBlock.gif                link.Add("000");
74InBlock.gif                Assert.AreEqual(link.Count, array.Length + 1"Error!");
75InBlock.gif            
76InBlock.gif                link.Remove("ddd");
77InBlock.gif                Assert.AreEqual(link.Count, array.Length, "Error!");
78InBlock.gif
79InBlock.gif                link.Insert(3"222");
80InBlock.gif                Assert.AreEqual(link.Count, array.Length + 1"Error!");
81InBlock.gif            
82InBlock.gif                link.RemoveAt(2);
83InBlock.gif                Assert.AreEqual(link.Count, array.Length, "Error!");
84ExpandedSubBlockEnd.gif            }

85InBlock.gif            
86InBlock.gif            end = DateTime.Now;
87InBlock.gif            passed = new TimeSpan(end.Ticks - start.Ticks);
88InBlock.gif            Console.Write(String.Format("End date time:{0}{1}", end.ToString("MM/dd/yyyy HH:mm:ss"), StringUtil.CrLf));
89InBlock.gif            Console.Write(string.Format("spend {0} seconds, {1} milliseconds {2}", passed.Seconds, passed.Milliseconds, StringUtil.CrLf));
90ExpandedSubBlockEnd.gif        }

91ExpandedSubBlockEnd.gif    }

92ExpandedSubBlockEnd.gif    #endregion

93ExpandedBlockEnd.gif}



运行结果如下:
 ------- 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 -------

 

转载于:https://www.cnblogs.com/soulroom/archive/2005/07/21/197192.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值