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></typeparam> /// <typeparam></typeparam> /// <param></param> /// <param></param> /// <param></param> /// <param></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></typeparam> /// <typeparam></typeparam> /// <param></param> /// <param></param> /// <param></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></typeparam> /// <param></param> /// <returns></returns> public int ChanListLength<T>(Node<T> head) { int count = 0;
(编辑:焦作站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|