相关数据结构的定义:
/// <summary> /// 线性表(顺序) /// </summary> public class SeqList { #region 顺序表初始化 ///<summary> /// 顺序表初始化 ///</summary> ///<param name="t"></param> public void SeqListInit<T>(SeqListType<T> t) { t.ListLen = 0; } #endregion #region 顺序表的长度 ///<summary> /// 顺序表的长度 ///</summary> ///<param name="t"></param> ///<returns></returns> public int SeqListLen<T>(SeqListType<T> t) { return t.ListLen; } #endregion #region 顺序表的添加 ///<summary> ///顺序表的添加 ///</summary> ///<param name="t"></param> ///<returns></returns> public bool SeqListAdd<T>(SeqListType<T> t, T data) { //防止数组溢出 if (t.ListLen == t.MaxSize) return false; t.ListData[t.ListLen++] = data; return true; } #endregion #region 顺序表的插入操作 ///<summary> /// 顺序表的插入操作 ///</summary> ///<param name="t"></param> ///<param name="n"></param> ///<param name="data"></param> ///<returns></returns> public bool SeqListInsert<T>(SeqListType<T> t, int n, T data) { //首先判断n是否合法 if (n < 0 || n > t.MaxSize - 1) return false; //说明数组已满,不能进行插入操作 if (t.ListLen == t.MaxSize) return false; //需要将插入点的数组数字依次向后移动 for (int i = t.ListLen - 1; i >= n; i--) { t.ListData[i + 1] = t.ListData[i]; } //最后将data插入到腾出来的位置 t.ListData[n] = data; t.ListLen++; return true; } #endregion #region 顺序表的删除操作 ///<summary> /// 顺序表的删除操作 ///</summary> ///<param name="t"></param> ///<param name="n"></param> ///<returns></returns> public bool SeqListDelete<T>(SeqListType<T> t, int n) { //判断删除位置是否非法 if (n < 0 || n > t.ListLen - 1) return false; //判断数组是否已满 if (t.ListLen == t.MaxSize) return false; //将n处后的元素向前移位 for (int i = n; i < t.ListLen; i++) t.ListData[i] = t.ListData[i + 1]; //去掉数组最后一个元素 --t.ListLen; return true; } #endregion #region 顺序表的按序号查找 ///<summary> /// 顺序表的按序号查找 ///</summary> ///<param name="t"></param> ///<param name="n"></param> ///<returns></returns> public T SeqListFindByNum<T>(SeqListType<T> t, int n) { if (n < 0 || n > t.ListLen - 1) return default(T); return t.ListData[n]; } #endregion #region 顺序表的关键字查找 ///<summary> /// 顺序表的关键字查找 ///</summary> ///<typeparam name="T"></typeparam> ///<typeparam name="W"></typeparam> ///<param name="t"></param> ///<param name="key"></param> ///<param name="where"></param> ///<returns></returns> public T SeqListFindByKey<T, W>(SeqListType<T> t, string key, Func<T, W> where) where W : IComparable { for (int i = 0; i < t.ListLen; i++) { if (where(t.ListData[i]).CompareTo(key) == 0) { return t.ListData[i]; } } return default(T); } #endregion } ///<summary> /// 定义一个顺序表的存储结构 ///</summary> public class SeqListType<T> { private const int maxSize = 100; public int MaxSize { get { return maxSize; } } //数据为100个存储空间 public T[] ListData = new T[maxSize]; public int ListLen { get; set; } }
使用:
private static void SeqListTest1() { SeqList seq = new SeqList(); SeqListType<Student> list = new SeqListType<Student>(); Console.WriteLine("\n********************** 添加2条数据 ************************\n"); seq.SeqListAdd<Student>(list, new Student() { ID = "1", Name = "Jack", Age = 23 }); seq.SeqListAdd<Student>(list, new Student() { ID = "3", Name = "Kate", Age = 23 }); Console.WriteLine("添加成功"); Display(list); Console.WriteLine("\n********************** 搜索Name=“Jack”的实体 ************************\n"); var student = seq.SeqListFindByKey<Student, string>(list, "Jack", s => s.Name); Console.WriteLine("\n********************** 搜索结果************************\n"); if (student != null) { Console.WriteLine("ID:" + student.ID + ",Name:" + student.Name + ",Age:" + student.Age); } else { Console.WriteLine("对不起,数据未能检索到。"); } Console.WriteLine("\n********************** 插入一条数据 ************************\n"); seq.SeqListInsert(list, 1, new Student() { ID = "2", Name = "博客园", Age = 40 }); Console.WriteLine("插入成功"); Display(list); Console.WriteLine("\n********************** 删除一条数据 ************************\n"); seq.SeqListDelete(list, 0); Console.WriteLine("删除成功"); Display(list); } static void Display(SeqListType<Student> list) { Console.WriteLine("\n********************** 显示数据 ************************\n"); if (list == null || list.ListLen == 0) { Console.WriteLine("没有数据"); return; } for (int i = 0; i < list.ListLen; i++) { Console.WriteLine("ID:" + list.ListData[i].ID + ",Name:" + list.ListData[i].Name + ",Age:" + list.ListData[i].Age); } }
Student类:
public class Student { public string ID { get; set; } public string Name { get; set; } public int Age { get; set; } }
下面看一下链表。
/// <summary> /// 链表 /// </summary> public class ChainList { #region 将节点添加到链表的末尾 /// <summary> /// 将节点添加到链表的末尾 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="head"></param> /// <param name="data"></param> /// <returns></returns> public Node<T> ChainListAddEnd<T>(Node<T> head, T data) { Node<T> node = new Node<T>(); node.data = data; node.next = null; ///说明是一个空链表 if (head == null) { head = node; return head; } //获取当前链表的最后一个节点 ChainListGetLast(head).next = node; return head; } #endregion #region 将节点添加到链表的开头 /// <summary> /// 将节点添加到链表的开头 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="chainList"></param> /// <param name="data"></param> /// <returns></returns> public Node<T> ChainListAddFirst<T>(Node<T> head, T data) { Node<T> node = new Node<T>(); node.data = data; node.next = head; head = node; return head; } #endregion #region 将节点插入到指定位置 /// <summary> /// 将节点插入到指定位置 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="head"></param> /// <param name="currentNode"></param> /// <param name="data"></param> /// <returns></returns> public Node<T> ChainListInsert<T, W>(Node<T> head, string key, Func<T, W> where, T data) where W : IComparable { if (head == null) return null; if (where(head.data).CompareTo(key) == 0) { Node<T> node = new Node<T>(); node.data = data; node.next = head.next; head.next = node; } ChainListInsert(head.next, key, where, data); return head; } #endregion #region 将指定关键字的节点删除 /// <summary> /// 将指定关键字的节点删除 /// </summary> /// <typeparam name="T"></typeparam> /// <typeparam name="W"></typeparam> /// <param name="head"></param> /// <param name="key"></param> /// <param name="where"></param> /// <param name="data"></param> /// <returns></returns> public Node<T> ChainListDelete<T, W>(Node<T> head, string key, Func<T, W> where) where W : IComparable { if (head == null) return null; //这是针对只有一个节点的解决方案 if (where(head.data).CompareTo(key) == 0) { if (head.next != null) head = head.next; else return head = null; } else { //判断一下此节点是否是要删除的节点的前一节点 if (head.next != null && where(head.next.data).CompareTo(key) == 0) { //将删除节点的next域指向前一节点 head.next = head.next.next; } } ChainListDelete(head.next, key, where); return head; } #endregion #region 通过关键字查找指定的节点 /// <summary> /// 通过关键字查找指定的节点 /// </summary> /// <typeparam name="T"></typeparam> /// <typeparam name="W"></typeparam> /// <param name="head"></param> /// <param name="key"></param> /// <param name="where"></param> /// <returns></returns> public Node<T> ChainListFindByKey<T, W>(Node<T> head, string key, Func<T, W> where) where W : IComparable { if (head == null) return null; if (where(head.data).CompareTo(key) == 0) return head; return ChainListFindByKey<T, W>(head.next, key, where); } #endregion #region 获取链表的长度 /// <summary> ///// 获取链表的长度 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="head"></param> /// <returns></returns> public int ChanListLength<T>(Node<T> head) { int count = 0; while (head != null) { ++count; head = head.next; } return count; } #endregion #region 得到当前链表的最后一个节点 /// <summary> /// 得到当前链表的最后一个节点 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="head"></param> /// <returns></returns> public Node<T> ChainListGetLast<T>(Node<T> head) { if (head.next == null) return head; return ChainListGetLast(head.next); } #endregion } #region 链表节点的数据结构 /// <summary> /// 链表节点的数据结构 /// </summary> public class Node<T> { /// <summary> /// 节点的数据域 /// </summary> public T data; /// <summary> /// 节点的指针域 /// </summary> public Node<T> next; } #endregion
使用例子:
private static void ChainListTest1() { ChainList chainList = new ChainList(); Node<Student> node = null; Console.WriteLine("将三条数据添加到链表的尾部:\n"); node = chainList.ChainListAddEnd(node, new Student() { ID = 2, Name = "Kate", Age = 23 }); node = chainList.ChainListAddEnd(node, new Student() { ID = 3, Name = "博客园", Age = 33 }); node = chainList.ChainListAddEnd(node, new Student() { ID = 5, Name = "Jack", Age = 23 }); Dispaly(node); Console.WriteLine("将ID=1的数据插入到链表开头:\n"); node = chainList.ChainListAddFirst(node, new Student() { ID = 1, Name = "i can fly", Age = 23 }); Dispaly(node); Console.WriteLine("查找Name=“Jack”的节点\n"); var result = chainList.ChainListFindByKey(node, "Jack", i => i.Name); DisplaySingle(node); Console.WriteLine("将”ID=4“的实体插入到“博客园”这个节点的之后\n"); node = chainList.ChainListInsert(node, "博客园", i => i.Name, new Student() { ID = 4, Name = "51cto", Age = 30 }); Dispaly(node); Console.WriteLine("删除Name=‘51cto‘的节点数据\n"); node = chainList.ChainListDelete(node, "51cto", i => i.Name); Dispaly(node); Console.WriteLine("获取链表的个数:" + chainList.ChanListLength(node)); } public static void Dispaly(Node<Student> head) { Console.WriteLine("******************* 链表数据如下 *******************"); var tempNode = head; while (tempNode != null) { Console.WriteLine("ID:" + tempNode.data.ID + ", Name:" + tempNode.data.Name + ",Age:" + tempNode.data.Age); tempNode = tempNode.next; } Console.WriteLine("******************* 链表数据展示完毕 *******************\n"); } public static void DisplaySingle(Node<Student> head) { if (head != null) { Console.WriteLine("ID:" + head.data.ID + ", Name:" + head.data.Name + ",Age:" + head.data.Age); } else { Console.WriteLine("未查找到数据!"); } }