工程目录结构:
Common.cs:
1 namespace DataStructure.Common 2 { 3 public class CommonClass 4 { 5 public const int DS_ListMaxSize = 20; //线性表的最大容量 6 } 7 }
DS_List.cs:
1 using System; 2 using DataStructure.Common; 3 using ElemType = System.Int32; // ElemType 用来指代数据结构中元素的数据类型,这样写便于更改 4 5 namespace DataStructure.List 6 { 7 8 /// <summary> 9 /// 自定义顺序存储结构线性表 10 /// </summary> 11 public class SqList 12 { 13 //需要注意的是:数组的索引是从0开始的,但是操作的位置是从1开始的 14 15 public ElemType[] array = new ElemType[CommonClass.DS_ListMaxSize]; //数组,存储线性表中的数据元素 16 public int length; //线性表长度 17 18 /// <summary> 19 /// 获取元素 20 /// </summary> 21 /// <param name="L"></param> 22 /// <param name="i">获取位置(1 开始)</param> 23 /// <param name="e">获取元素的值</param> 24 /// <returns></returns> 25 public bool GetElem(SqList L, int i, ref ElemType e) 26 { 27 if (L.length <= 0 || i < 1 || i > L.length) 28 return false; 29 e = L.array[i - 1]; 30 return true; 31 } 32 33 /// <summary> 34 /// 插入元素 35 /// </summary> 36 /// <param name="L"></param> 37 /// <param name="i">插入位置(1 开始)</param> 38 /// <param name="e">要插入的元素的值</param> 39 /// <returns></returns> 40 public bool ListInsert(SqList L, int i, ElemType e) 41 { 42 if (L.length == CommonClass.DS_ListMaxSize) 43 { 44 Console.WriteLine("线性表已满"); 45 return false; 46 } 47 48 if (i < 0 || i > L.length + 1) 49 { 50 Console.WriteLine("插入位置不合理"); 51 return false; 52 } 53 54 if (i <= L.length) 55 { 56 for (int k = L.length - 1; k >= i - 1; --k) 57 L.array[k + 1] = L.array[k]; 58 } 59 60 L.array[i - 1] = e; 61 ++L.length; 62 63 return true; 64 } 65 66 /// <summary> 67 /// 删除元素 68 /// </summary> 69 /// <param name="L"></param> 70 /// <param name="i">删除位置(1 开始)</param> 71 /// <param name="e">要删除的元素的值</param> 72 /// <returns></returns> 73 public bool DeleteElem(SqList L, int i, ref ElemType e) 74 { 75 if (L.length <= 0) 76 { 77 Console.WriteLine("线性表为空"); 78 return false; 79 } 80 81 if (i < 1 || i > L.length) 82 { 83 Console.WriteLine("删除位置不合理"); 84 return false; 85 } 86 87 e = L.array[i - 1]; 88 89 for (int k = i, kMax = L.length; k < kMax; ++k) 90 { 91 L.array[k - 1] = L.array[k]; 92 } 93 94 --L.length; 95 96 return true; 97 } 98 } 99 100 /// <summary> 101 /// 用来测试自定义的顺序线性表 102 /// </summary> 103 public class TestSqList 104 { 105 public void Show() 106 { 107 SqList sqList = new SqList(); 108 109 //插入元素 110 for (int i = 1; i <= 10; i++) 111 { 112 sqList.ListInsert(sqList, i, i * 2); 113 } 114 115 //打印元素 116 for (int i = 1, iMax = sqList.length; i <= iMax; ++i) 117 { 118 int value = -1; 119 if (sqList.GetElem(sqList, i, ref value)) 120 { 121 Console.WriteLine(value); 122 } 123 } 124 125 int temp = -1; 126 if (sqList.DeleteElem(sqList, 2, ref temp)) 127 { 128 Console.WriteLine("删除的元素是 : " + temp); 129 } 130 131 //打印元素 132 for (int i = 1, iMax = sqList.length; i <= iMax; ++i) 133 { 134 int value = -1; 135 if (sqList.GetElem(sqList, i, ref value)) 136 { 137 Console.WriteLine(value); 138 } 139 } 140 141 } 142 } 143 }
progam.cs:
1 using System; 2 using DataStructure.List; 3 4 namespace DataStructure 5 { 6 class Program 7 { 8 static void Main(string[] args) 9 { 10 #region 测试顺序线性 11 12 TestSqList temp = new TestSqList(); 13 temp.Show(); 14 15 #endregion 16 17 Console.ReadKey(); 18 } 19 } 20 }
原文地址:https://www.cnblogs.com/luguoshuai/p/9238909.html
时间: 2024-10-03 23:12:49