static void TestLink() { //增加一个元素,删除两个元素 MyLink <int> myLink = new MyLink <int>(); myLink.Add(2); PrintLink(myLink, "增加一个元素后:"); myLink.DeleteByIndex(1); PrintLink(myLink, "删除一个元素后:"); myLink.DeleteByIndex(1); PrintLink(myLink, "再删除空链表的一个元素后:"); Console.WriteLine(); //快速增加元素与插入元素 myLink.AddQuick(5); PrintLink(myLink, "快速添加3个元素后:"); myLink.Insert(1, 80); PrintLink(myLink, "向索引1前插入80后:"); myLink.DeleteByIndex(1); myLink.DeleteByIndex(1); PrintLink(myLink, "连续删除第一个元素后:"); Console.WriteLine(); //在头部添加两个元素,在快速插入一个 myLink.AddToHead(20); PrintLink(myLink, "第一次在头部添加元素后:"); myLink.AddToHead(10); PrintLink(myLink, "第二次在头部添加元素后:"); myLink.AddQuick(30); PrintLink(myLink, "快速添加元素后:"); myLink.DeleteByIndex(3); PrintLink(myLink, "删除第三个元素后:"); myLink.AddQuick(5); PrintLink(myLink, "在快速添加元素5:"); Console.WriteLine(); //批量添加和插入元素 int[] array = { 1, 3, 6 }; myLink.AddVolume(array); PrintLink(myLink, "批量插入1,3,6后:"); myLink.InsertVolume(2, array); PrintLink(myLink, "在索引2前批量插入1,3,6后:"); myLink.Add(55); myLink.AddQuick(66); PrintLink(myLink, "添加55,和快速添加66后:"); Console.WriteLine(); //索引非法检测 int data = 0; myLink.GetElement(0, ref data); myLink.GetElement(100, ref data); myLink.Insert(30, 20); myLink.DeleteByIndex(-1); myLink.DeleteByIndex(200); }
static void PrintLink(MyLink <int> l, string text = "输出:") { int data = 0; Console.Write(text); for (int i = 0; i < l.GetLinkLength(); i++) { l.GetElement(i + 1, ref data); Console.Write(data.ToString() + " "); } Console.WriteLine(); }