public SqList <T> Create(T[] array) { SqList <T> sqList = new SqList <T>(); for (int i = 0; i < array.Length; i++) { Insert(sqList, i, array[i]); } return(sqList); }
/// <summary> /// 顺序表:插入元素 /// </summary> /// <param name="sqList"></param> /// <param name="pos"></param> /// <param name="data"></param> /// <returns></returns> public bool Insert(SqList <T> sqList, int pos, T data) { if (pos < 0 || pos > sqList.length || sqList.length == GlobalVariable.MaxSize) { return(false); } for (int i = sqList.length - 1; i >= pos; i--) { sqList.data[i + 1] = sqList.data[i]; } sqList.data[pos] = data; ++sqList.length; return(true); }
/// <summary> /// 顺序表:删除元素 /// </summary> /// <param name="sqList"></param> /// <param name="pos"></param> /// <param name="data"></param> /// <returns></returns> public bool Delete(SqList <T> sqList, int pos, out T data) { if (pos < 0 || pos > sqList.length - 1) { data = default; return(false); } data = sqList.data[pos]; for (int i = pos; i < sqList.length - 1; i++) { sqList.data[i] = sqList.data[i + 1]; } --sqList.length; return(true); }
private static void TestSequenceList() { SequenceList <int> sequenceList = new SequenceList <int>(); int[] array = { 8, 1, 2, 3, 4, 5, 6, 7 }; SqList <int> sqList = sequenceList.Create(array); sequenceList.Print(sqList); Console.WriteLine("\n" + sqList.length); sequenceList.Insert(sqList, 1, 10); sequenceList.Print(sqList); sequenceList.Delete(sqList, 5, out int data); sequenceList.Print(sqList); Console.WriteLine("\n值:" + data); int node = sequenceList.GetElem(sqList, 2); Console.WriteLine("\n值:" + node); int index = sequenceList.GetIndex(sqList, 3); Console.WriteLine("\n索引:" + index); }
/// <summary> /// 顺序表:求指定位置元素的算法;返回目标查找元素 /// </summary> /// <param name="sqList"></param> /// <param name="pos"></param> /// <returns></returns> public T GetElem(SqList <T> sqList, int pos) { if (pos < 0 || pos > sqList.length - 1) { return(default);