数据结构C#版线性表(Data Structure)之顺序表(顺序表(SeqList)的实现)
2010-10-17 12:21:23 来源:WEB开发网核心提示:测试代码片段:Console.WriteLine("顺序表测试开始...");SeqList<string> seq = new SeqList<string>(10);seq.Append("x");seq.InsertBefore("w&qu
测试代码片段:
Console.WriteLine("顺序表测试开始..."); SeqList<string> seq = new SeqList<string>(10); seq.Append("x"); seq.InsertBefore("w", 0); seq.InsertBefore("v", 0); seq.Append("y"); seq.InsertBefore("z", seq.Count()); Console.WriteLine(seq.Count());//5 Console.WriteLine(seq.ToString());//v,w,x,y,z Console.WriteLine(seq[1]);//w Console.WriteLine(seq[0]);//v Console.WriteLine(seq[4]);//z Console.WriteLine(seq.IndexOf("z"));//4 Console.WriteLine(seq.RemoveAt(2));//x Console.WriteLine(seq.ToString());//v,w,y,z seq.InsertBefore("x", 2); Console.WriteLine(seq.ToString());//v,w,x,y,z Console.WriteLine(seq.GetItemAt(2));//x seq.Reverse(); Console.WriteLine(seq.ToString());//z,y,x,w,v seq.InsertAfter("z_1", 0); seq.InsertAfter("y_1", 2); seq.InsertAfter("v_1", seq.Count()-1); Console.WriteLine(seq.ToString());//z,z_1,y,y_1,x,w,v,v_1
顺序表的优点:读取元素时可直接定位,所以在某些操作(比如将顺序表元素反转合围)中,不需要完全遍历,循环次数(即时间复杂度)相对完全遍历而言能减少一半。
顺序表的优点:插入/删除元素,因为要保持其顺序性,所以后续元素需要移动,增加了时间开销。
数据结构C#版笔记--线性表(Data Structure)之单链表(LinkList)
更多精彩
赞助商链接