/// <summary> /// 去除顺序表的重复项 /// </summary> /// <param name="la"></param> static SequenceList <int> PurgeList(SequenceList <int> la) { SequenceList <int> myList = new SequenceList <int>(la.MaxSize); int j = 0; bool ifHas = false; for (int i = 0; i < la.GetLength(); i++) { j = 0; ifHas = false; while (j < la.GetLength()) { if (i != j && la[i] == la[j]) { ifHas = true; break; } j++; } if (ifHas == false) { myList.Append(la[i]); } } return(myList); }
/// <summary> /// 倒置顺序表 /// </summary> /// <param name="la">顺序表la</param> /// <param name="lb">顺序表lb</param> static SequenceList <int?> MergeListPro(SequenceList <int?> la, SequenceList <int?> lb) { SequenceList <int?> lc = new SequenceList <int?>(la.MaxSize + lb.MaxSize); int a = 0, b = 0; //注意:一次循环插入la,lb中各一个数据 while (a < la.GetLength() && b < lb.GetLength()) { if (la[a] < lb[b]) { lc.Append(la[a++]); } else { lc.Append(lb[b++]); } } //la 没有循环完毕,把剩余的la添加到lc中 while (a < la.GetLength()) { lc.Append(la[a++]); } //lb 没有循环完毕,把剩余的lb添加到lc中 while (b < lb.GetLength()) { lc.Append(lb[b++]); } return(lc); }
/// <summary> ///Delete 的测试 ///</summary> public void DeleteTestHelperString() { int size = 10; // TODO: 初始化为适当的值 SequenceList <String> target = new SequenceList <string>(size); // TODO: 初始化为适当的值 List <string> list = new List <string>() { "0", "1", "2", "3", "4", "5" }; foreach (var item in list) { target.Append(item); } target.Delete(0); target.Delete(2); target.Delete(3); Assert.AreEqual(3, target.GetLength()); Assert.AreEqual("1", target[0]); Assert.AreEqual("2", target[1]); Assert.AreEqual("4", target[2]); }
/// <summary> ///GetLength 的测试 ///</summary> public void GetLengthTestHelper <T>() { int size = 5; // TODO: 初始化为适当的值 SequenceList <T> target = new SequenceList <T>(size); // TODO: 初始化为适当的值 int expected = 0; // TODO: 初始化为适当的值 int actual; actual = target.GetLength(); Assert.AreEqual(expected, actual); target.Append(default(T)); Assert.AreEqual(1, target.GetLength()); target.Append(default(T)); Assert.AreEqual(2, target.GetLength()); }
/// <summary> /// 倒置顺序表 /// </summary> /// <param name="sqList"></param> static SequenceList <int> ReverseList(SequenceList <int> sqList) { SequenceList <int> myList = new SequenceList <int>(sqList.MaxSize); int length = sqList.GetLength(); for (int i = 1; i <= length; i++) { myList.Append(sqList[length - i]); } return(myList); }
/// <summary> ///Clear 的测试 ///</summary> public void ClearTestHelper <T>() { int size = 10; // TODO: 初始化为适当的值 SequenceList <T> target = new SequenceList <T>(size); // TODO: 初始化为适当的值 target.Append(default(T)); target.Clear(); Assert.AreEqual(-1, target.Last); Assert.AreEqual(0, target.GetLength()); //Assert.IsNull(target.Data); }
/// <summary> /// 合并顺序表 /// </summary> /// <param name="la"></param> /// <param name="lb"></param> static SequenceList <int?> MergeList(SequenceList <int?> la, SequenceList <int?> lb) { //合并后的表长度 length=la.MaxSize+lb.MaxSize-1 int length = la.MaxSize + lb.MaxSize; SequenceList <int?> lc = new SequenceList <int?>(length); //复制La到myList for (int i = 0; i < la.GetLength(); i++) { lc.Append(la[i]); } //Lb中的值与myList比较,如果当前值Lb[i]比myList的某个位置的值相等或等于, //那么lb[i]插入到mylist对应的位置中,否则lb[i]放到mylist的末尾。 for (int i = 0; i < lb.GetLength(); i++) { //是否找到lb[i]<=mylist[j]的标志 bool find = false; //开始查找位置 int startLocation = 0; for (int j = startLocation; j < length; j++) { if (lc[j].HasValue && lb[i] <= lc[j]) { find = true; lc.Insert(lb[i], j); startLocation = j; break; } } //如果找不到 if (!find) { lc.Append(lb[i]); } } return(lc); }
public void AppendTestHelperString() { int size = 5; // TODO: 初始化为适当的值 SequenceList <string> target = new SequenceList <string>(size); // TODO: 初始化为适当的值 List <string> myList = new List <string>() { "a", "b", "c", "d" }; foreach (string item in myList) { target.Append(item); } Assert.AreEqual(myList.Count, target.GetLength()); for (int i = 0; i < myList.Count; i++) { Assert.AreEqual(myList[i], target[i]); } }